shell script how to switch users?

Jacques B. jjrboucher at gmail.com
Thu Jan 4 14:03:32 UTC 2007


> Use the -c option with su, and call a new script to run as that user.

In case you aren't sure about that option, use it as follows:

su user2 -c user2script.sh

If you only want to run one command as user2, you could simply insert
that command after the -c instead.

su user2 -c whoami

A side note, if the command is more than one word, you must enclose it
in quotes. For example:

su -c 'find / -name \*.c -print' user2
(obviously the order of -c command and the user is interchangeable).

Jacques B.




More information about the fedora-list mailing list