Use the result of 'whoami' as part of an alias

1

I am setting up my default .bashrc on my Ubuntu machine that I will soon be adding more and more users to.

One command that users will have access to is mysql, logging them into the SQL system on the server. Is there a was yo dynamically update the .bashrc depending on the username without human intervention?

What I am looking for is to set up an alias for the following:

User Matt will have an alias:

alias sql='mysql -u matt -p'

User Jim will have an alias:

alias sql='mysql -u jim -p'

The mysql username will always be the same as the system username, making me wonder if there was a simple way to get the output of whoami and use that as part of the command.

Matt Clark

Posted 2013-08-14T22:06:57.863

Reputation: 1 819

Answers

3

You could use /etc/skel and put a .bashrc inside with

alias sql='mysql -u `whoami` -p';

user245743

Posted 2013-08-14T22:06:57.863

Reputation: 54

Why call the command whoami when you can use the USER environmental variable? – justbrowsing – 2013-08-15T05:59:16.260

1`` is deprecated. you may consider to use $(whoami) instead. Also there is no need for ; at the end. Just write everything in one line. A function might be more flexible. – l1zard – 2013-08-15T07:05:08.010

3

Use a function instead of abusing alias

sql() { mysql -u $USER -p "$@"; }

Add to /etc/skel/.bashrc for new users and /etc/bash.bashrc for existing users.

justbrowsing

Posted 2013-08-14T22:06:57.863

Reputation: 2 411