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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-614.png" data-lazy- height="459" src="data:image/svg xml,” width=”1022″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-615.png" data-lazy- height="263" src="data:image/svg xml,” width=”1022″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-616.png" data-lazy- height="151" src="data:image/svg xml,” width=”602″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-617.png" data-lazy- height="151" src="data:image/svg xml,” width=”602″>

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

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-618.png" data-lazy- height="179" src="data:image/svg xml,” width=”546″>

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

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-619.png" data-lazy- height="179" src="data:image/svg xml,” width=”546″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-620.png" data-lazy- height="148" src="data:image/svg xml,” width=”680″>

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

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-621.png" data-lazy- height="235" src="data:image/svg xml,” width=”700″>

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

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-622.png" data-lazy- height="235" src="data:image/svg xml,” width=”728″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-623.png" data-lazy- height="235" src="data:image/svg xml,” width=”728″>

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

su -l

echo $LOGNAME

echo $USER

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/word-image-624.png" data-lazy- height="235" src="data:image/svg xml,” width=”728″>

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

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/linuxinstitute_icono-150×150.png61214b42ceeb4.jpg" height="112" src="data:image/svg xml,” width=”112″>

David Adams

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