Bash su vs su – commands

Working with the command line, you sometimes need to drop to root in order to carry out administrative tasks.

You can either use su or su –, but what is the difference? Firstly lets look at the path (where bash looks for programs etc) with a normal user.

psutton@Desktop:~$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

If we use the su command then, we end up with the same path, this explains why some programs won't run.

psutton@Desktop:~$ su
Password: 
root@Desktop:/home/psutton# echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
root@Desktop:/home/psutton# 

If we use the su – command then bash picks up the path set up for the root account, so that path points to some of the directories containing admin tools.

psutton@Desktop:~$ su -
Password: 
root@Desktop:~# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
root@Desktop:~# exit

Both of these scenarios are useful, depending on what you want to do as root user.


Mastodon ShellLabs Join Mastodon
AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay