Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 2 down vote accepted

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

alias sql='mysql -u `whoami` -p';
share|improve this answer
    
Why call the command whoami when you can use the USER environmental variable? – justbrowsing Aug 15 '13 at 5:59
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 Aug 15 '13 at 7:05

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.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .