Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

How can I let sudo use my existing aliases in my .bash_profile (or .bashrc, .profile …)? For example I want to be able to use ll instead of ls -laGF as sudo.

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

No. Sudo draws from the root account and won't consult your user's ~/.bash_profile.

But you can workaround this problem by adding the following alias to your profile:

alias sudo='sudo '

*Note the space after the command. From then on, sudo will work normally and you'll be able to use your user's aliases without issue (both normally and with root privileges).


Explanation: http://www.gnu.org/software/bash/manual/bashref.html#Aliases says:

If the last character of the alias value is a space or tab character, then the next command word following the alias is also checked for alias expansion.

share|improve this answer
    
I suppose you meant to type `alias sudo='sudo ', since that works! Change your answer accordingly and I will accept it. –  Asmus Mar 24 '12 at 19:05
    
@Asmus yes sorry. Fixed. –  cksum Mar 24 '12 at 20:45
add comment

alias sudo='sudo su -i ENV=/home/myuser/.profile'

share|improve this answer
add comment

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.