Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

2 Answers 2

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.