140

I'm quite new to Linux terminal and I'm not quite sure what the difference between su with a hyphen and su without a hyphen is, for example: su - username vs. su username.

I looked into the documentation but in there, this was not mentioned. Could someone please help me out?

user574183
  • 1,519

2 Answers2

131

The difference between "-" and "no hyphen" is that the latter keeps your existing environment (variables, etc); the former creates a new environment (with the settings of the actual user, not your own).

https://wiki.archlinux.org/index.php/Su

The hyphen has two effects:

1) switches from the current directory to the home directory of the new user (e.g., to /root in the case of the root user) by logging in as that user

2) changes the environmental variables to those of the new user as dictated by their ~/.bashrc. That is, if the first argument to su is a hyphen, the current directory and environment will be changed to what would be expected if the new user had actually logged on to a new session (rather than just taking over an existing session).

paulsm4
  • 1,949
11

Year 2022 update:

It is discouraged shorthand for --login.

See: https://man.archlinux.org/man/su.1#DESCRIPTION

It is recommended to always use the --login option (instead of its shortcut -) to avoid side effects caused by mixing environments.

And https://wiki.archlinux.org/title/Su :

You will sometimes encounter su being used to get a login shell with su -, rather than su -l/su --login. This shorthand is discouraged because the - option can encounter parsing limitations.

user124
  • 221