Shell Person Help me keep the shell people alive.

19Apr/109

Using sudo with an alias

Typically sudo will ignore any aliased commands from your .bashrc, .bash_aliases, or the alias command. For example, I use "ll" as an alias for "ls -lh". Typing "ll" will give me a long-listing of a directory's contents, while typing "sudo ll" will give me:

sudo: ll: command not found

I learned this when I tried to create an alias for "shutdown" that would refuse to shutdown if rtorrent was running. Unfortunately you need root privileges to use /sbin/shutdown, and sudo would completely ignore the clever script I aliased as "shutdown". The solution is an additional alias:

alias sudo='sudo '

The space following "sudo" tells bash to check if the command that follows the space is also an alias. From the bash man page:

man bash

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

Now my aliased shutdown script is called even though it's being run with sudo. I've heard of another solution (also an alias), but I haven't tried it (1) because this one works just fine, and (2) because I don't understand why it works (if it does work). Here it is:

alias sudo='A=`alias` sudo  '

I'm not sure if it's just the hard way of doing things, or if it's somehow better, worse, or different.

Here's a copy of my current .bash_aliases file. This is a fresh install, so I'll probably add a lot to it in the future.

# Shortcuts
alias ll='ls -lh'
alias la='ls -lhA'
alias l='ls'
alias c='clear'
alias x='exit'
alias q='exit'

# Don't run shutdown if rtorrent is running - as long as there's a screen with "tor" in its name, shutdown won't run (unless you call /sbin/shutdown, or unalias it)
alias shutdown='/home/james/scripts/safe.shutdown.sh'

# When using sudo, use alias expansion (otherwise sudo ignores your aliases)
alias sudo='sudo '
Comments (9) Trackbacks (2)
  1. Thanks you for the: alias sudo=’sudo ‘

    It helped a lot.

  2. Glad it helped.

  3. Both work great, but I’m really puzzled by the “A=” part, anyone has any idea about it ?

  4. Big, Big, thanks for that space!

    IT IS INEXCUSABLE THAT THIS ALIAS IS NOT PRESENT AS DEFAULT ON SUDO-based DISTROS !!!
    GRRR

    (Was forced to use sudo-i fo half a year because of this stupidity.)

  5. Completely agree. Also it’s annoying how hard it is to use sudo with functions (instead of aliases) (someone let me know if there’s an easy way).

  6. saved my time, thanks a lot!

  7. Glad to help.

  8. thanks, but thanks.

    Very intelligent: alias sudo=’sudo ‘

  9. In Response to:

    shell.person:

    Completely agree. Also it’s annoying how hard it is to use sudo with functions (instead of aliases) (someone let me know if there’s an easy way).

    What is hard about using sudo with functions compared to aliases? Functions can actually be exported “export -f function” and then used with sudo without any fuss. I’m a big fan of exporting function though it’s not very popular and many people are unaware it’s even possible.


Leave a comment

(required)