Bash Fu #1

Jeff Mancuso June 23rd, 2008

Ever 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

  • http://chigeek.com Steven McGrath

    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

  • Jeff Mancuso

    Steven – thanks for that bit, I didn’t know about that parameter

Grab the RSS feed
Follow ExpanDrive on Twitter Follow us on Twitter


Try ExpanDrive

If you’ve heard of SSH then you need ExpanDrive.



Try Strongspace

Awesome SFTP/rsync online storage by ExpanDrive.