Bash Fu #1
June 23rd, 2008Ever need execute a command in the context of another user while running as root? Like running sudo from an unpriviledged account – except the other way around.
I had always thought ‘su‘ stood for “super user”, but as it turns out it actually is an acronym for “substitute user” – and it can do exactly what we want.
To execute a command in the context of another user, while running as superuser, you can run su -c, followed by the command, followed by ' - user'.
For example, if I wanted to make a script to pull in the contents of a git repository that is stored in the context of user ‘www’ I could run
su -c \"git --git-dir='/projects/www/.git' pull\" - www
For some more info on su check here



June 25th, 2008 at 2:42 pm
You do realize that sudo can run something from ANY user. In OSX as long as your an admin user, you have full rights to do anything with sudo.
sudo -u USERNAME
June 26th, 2008 at 8:47 am
Steven – thanks for that bit, I didn’t know about that parameter