Skip to content
koalaman edited this page Feb 17, 2014 · 21 revisions

"To run commands as another user, use su -c or sudo.

Problematic code:

./configure
make
su root
make install

Correct code:

./configure
make
sudo make install  # or su -c 'make install' root

Rationale:

su doesn't actually switch user. It starts a brand new shell, running as another user. You can't put su foo in a script to make the following command run as user foo.

Use sudo -u username cmd or su -c cmd username instead.

Contraindications

If you want to present the user with a root shell or when you're redirecting input, you can ignore this message:

until mycommand
do
  echo "Failed. You will now get a root shell. Fix problem and exit to retry."
  su
done

or

exec < mycommands
su foo

ShellCheck

Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature above to find a specific one, or see Checks.

Clone this wiki locally