How can I execute commands as another user with their .bash_profile instead of the current user's?

Jesse picture Jesse · Feb 22, 2012 · Viewed 9.2k times · Source

I want to run a command as a not-root user in a startup script (rc.local). The command I want to execute is actually an alias in the non-root user's .bash_profile. I have tried to use

su - myuser -c aliased_cmd

but this doesn't work. If I simply type

su - myuser
# wait for login...
aliased_cmd

it works fine, but obviously this isn't appropriate in the actual script. Of course I could copy the aliases and functions I'd like to use into the actual rc.local file, but this seems a bit silly... perhaps there is an option I don't know about or a way to use sudo for this?

Answer

Alex picture Alex · Feb 22, 2012

after sourcing the profile, include the following

shopt -s expand_aliases