Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I recently switched from tcsh to bash, and I'm used to being able to do things like sudo `alias netstat` but since alias gives name=value in bash, I can't do this anymore. Is there an equivalent in bash, so I don't have to do sudo `alias netstat | sed -r "s/.*='(.*)'/\1/"`?

share|improve this question
Wrap it in a function and add it to .bashrc. Problem solved. – Daniel Beck Jan 1 '12 at 9:10
up vote 2 down vote accepted

You're trying to have bash expand aliases after sudo. You do not need to do it the exact same way; in fact, there is a far more convenient way in bash – add an alias for sudo that ends with a space...

alias sudo="sudo "

...and sudo netstat will be expanded automatically.

share|improve this answer
excellent, and sudo \netstat won't expand netstat's alias! – Jayen Jan 1 '12 at 13:41

Bash stores its list of aliases in the associative array BASH_ALIASES. The equivalent of sudo `alias netstat` is then sudo ${BASH_ALIASES[netstat]}. However, I would suggest the following instead, which works with builtin shell commands and deals correctly with quoting:

sudo bash -c "${BASH_ALIASES[netstat]}"

There's still a lot that won't work with this, like e.g. nested aliases.

share|improve this answer

Your Answer


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.