Linux Commands

su vs. su –

The difference between the su and the hyphenated su – commands is the su command without arguments keeps almost all environment variables belonging to the original user. Contrary to this, the hyphenated su – command clears most environment variables.

We are not talking about two different commands but about a command without a specific flag (the hyphen is a flag) and the same command with the flag.

Typing su – is the same as typing su -l; both commands will clear most of the environment variables. Thus, before continuing with this article, let’s see what an environment variable is.

Environment variables in Linux:

The environment variables are values used to define characteristics of the environment in which shell processes and their child processes are being executed, for example, processes use the environment variable $SHELL to know the shell in use (Bash, Zsh, etc.).

The system has many variables; this article will focus on those variables affected when we use the su command.

To see all variables in your system, you can run the export command as shown below.

export

As you see in the screenshot above, there are many variables in our system. We will focus on the specific variables HOME, SHELL, USER, LOGNAME, and PATH.

The first variable we’ll see is PATH. The values of this environment variable are useful for the system to know where to find the commands or programs to execute. The default system’s environment variable PATH is defined in the /etc/profile file. Users’ PATH variables are located within their home directories at ~/.profile.

To learn your PATH environment variable values, you can use the echo command followed by the $ symbol and the environment variable you want to check, in this case, PATH, as shown in the example below.

echo $PATH

As you can see, the system returns the defined environment variables for our system to know where to search and find the commands we want to execute.

The second environment variable explained in this article is the SHELL variable. The value of this variable tells the system what shell we are using, if Bash, Zsh, Ksh, etc.

To see the value of your SHELL environment variable, repeat the step explained previously, replacing $PATH with $SHELL, as shown in the screenshot below.

echo $SHELL

And as you can see, the shell is Bash. That’s how our system knows what shell we are using, with the value shown above.

Another important environment variable in Linux is $HOME. This value indicates what the home directory of the user is. To learn this value, you can run the command below.

echo $HOME

The environment variable $ LOGNAME’s value is the user you’re logged in as.

echo $LOGNAME

The $USER environment variable value is the user ID, which is the same as $LOGNAME.

echo $USER

The environment variables explained above are the ones affected by the su command when the hyphen or the -l flag are added.

Difference between “su” and “su -” commands:

The difference between the su command without flags and su with a hyphen or -l is the command without flags keeps your environment variables and only changes the user.

When used with hyphens or the -l flag, most environment variables are cleared.

The next example shows the linuxhint user switching to root using su without options.

Then, after checking the $LOGNAME environment variable, we see it remains as linuxhint and not root.

su

echo $LOGNAME

Contrary to the previous example, if the hyphen is implemented, it will be updated to root when checking the $LOGNAME variable.

su -

echo $LOGNAME

Let’s try the same with the $USER environment variable.

su

echo $USER

As you can see, when doing su as linuxhint to change to root and checking the $USER variable, it remains as linuxhint.

Contrary to the previous example, if implementing the hyphen, the $USER variable will be cleared and updated as shown below.

su -

echo $USER

To end this tutorial, this last example shows the -l flag does exactly the same as the hyphen.

su -l

echo $LOGNAME

echo $USER

Conclusion:

Environment variables are a basic concept any Linux user must know. Once you understand their function, you can easily realize the difference between the su command without and with the -l flag (which is the same as the hyphenated su). If you liked this tutorial, you might want to read Managing sudo privileges on Linux.

I hope this article showing the difference between “su” and “su –” was helpful. Keep following this blog for more Linux tutorials and tips.

About the author

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.