Last Updated: February 25, 2016
·
3.191K
· amoniker

Use your aliases with sudo

Normally, when using sudo, you don't have access to aliases that have been defined for your user. These aliases are useful of course, so it's nice to make them available in a sudo context.

To do so, simply add this line to your .bash_profile:

alias sudo="sudo " - note the space after the second sudo

How does this work?

When running a command, only the first word is checked against your aliases. So myalias by itself will work, but running sudo myalias means that only sudo is checked against your aliases - not the second word myalias.

The trick of aliasing sudo to itself (plus a space!) works because "[when] 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." - from the bash manual

This way, sudo myalias will check both words against your aliases.

Enjoy!