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 have added alias ll='ls -la' in /etc/bash.bashrc but sudo ll is not working. Is there a way to "source" bash.bashrc? In a related post, another user suggesting creating an alias for sudo with a space after it. Eg. alias sudo='sudo ' but there wasn't any context to this answer. Should I add this to my bashrc? Seems hackish.

What is the correct method for adding an alias for my sudo one-off commands?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Yes, adding the alias sudo='sudo ' line will fix your problem. For a better definition (from the bash manual),

Aliases allow a string to be substituted for a word when it is used as the first word of a simple command. The shell maintains a list of aliases that may be set and unset with the alias and unalias builtin commands.

The first word of each simple command, if unquoted, is checked to see if it has an alias. If so, that word is replaced by the text of the alias. The characters ‘/’, ‘$’, ‘`’, ‘=’ and any of the shell metacharacters or quoting characters listed above may not appear in an alias name. The replacement text may contain any valid shell input, including shell metacharacters. The first word of the replacement text is tested for aliases, but a word that is identical to an alias being expanded is not expanded a second time. This means that one may alias ls to "ls -F", for instance, and Bash does not try to recursively expand the replacement text. 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.

One way you can be sure is typing it in a terminal window.

If you do it this way, the changes aren't permanent, they only exist until you close/logout of the terminal.

I think, in your case, it would be safer to add the changes to ~/.bashrc instead of /etc/bash.bashrc, but it really doesn't make much of a difference.

share|improve this answer
    
Yes, I have deleted my entry /etc/bash.bashrc and added alias sudo='sudo ' in my ~/.bashrc and everything works the way I want it to. Thanks! –  curios Feb 4 '14 at 18:59
    
You're welcome! –  IBPX May 5 '14 at 21:32

Try adding the alias to ~/.bashrc instead, that is the initalization file that bash is going to look at per user. I think editing that file will make the alias system wide.

For example, in ~/.bashrc, I would set the following sudo apt-get commands on most Ubuntu installs

alias apts='sudo apt-cache search'
alias apti='sudo apt-get install'
alias aptr='sudo apt-get remove'

What command specifically are you trying to make an alias for? usually an alias is a way to make what would be a long complicated command a simple one word command.

Also, why are you trying to sudo a command that does not usually require sudo like ls -la? I think when you run a command via sudo it changes the environmental variables available to you, so that may be part of your issue as well.

share|improve this answer
    
He is trying to make typing ll run the command ls -la. He did this right, but he is wondering how to run it with sudo. (i.e. if the folder is read-protected.) –  IBPX Feb 4 '14 at 18:51
    
The settings.json file for a program I am configuring is listed in a place where my user doesn't have read permission. So bash asks for permission after I ll that directory. sudo !! fails because sudo doesn't have the ll alias. Hence my question. –  curios Feb 4 '14 at 19:00
    
Ah ok, didnt think about that.. –  Richie086 Feb 4 '14 at 22:15

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.