How to pass alias through sudo

Posted by Tanktalus on Server Fault See other posts from Server Fault or by Tanktalus
Published on 2009-09-01T20:44:48Z Indexed on 2011/06/22 16:23 UTC
Read the original article Hit count: 314

Filed under:
|
|

I have an alias that passes in some parameters to a tool that I use often. Sometimes I run as myself, sometimes under sudo. Unfortunately, of course, sudo doesn't recognise the alias.

Does anyone have a hint on how to pass the alias through?

In this case, I have a bunch of options for perl when I'm debugging:

alias pd='perl -Ilib -I/home/myuser/lib -d'

Sometimes, I have to debug my tools as root, so, instead of running:

pd ./mytool --some params

I need to run it under sudo. I've tried many ways:

sudo eval $(alias pd)\; pd ./mytool --some params
sudo $(alias pd)\; pd ./mytool --some params
sudo bash -c "$(alias pd)\; pd ./mytool --some params"
sudo bash -c "$(alias pd); pd ./mytool --some params"
sudo bash -c eval\ "$(alias pd)\; pd ./mytool --some params"
sudo bash -c eval\ "'$(alias pd)\; pd ./mytool --some params'"

I was hoping for a nice, concise way to ensure that my current pd alias was fully used (in case I need to tweak it later), though some of my attempts weren't concise at all. My last resort is to put it into a shell script and put that somewhere that sudo will be able to find. But aliases are soooo handy sometimes, so it is a last resort.

© Server Fault or respective owner

Related posts about shell

Related posts about sudo