How And Why You Would Use The $SHLVL Variable

Linux shell levels can get complicated. $SHLVL can help

The $SHLVL variable tells you how many shells deep you are. If you are confused by this, it is worth starting at the beginning.

Screenshot of $SHLVL Linux command in use

What Is a Shell?

A shell takes commands and gives these commands to the underlying operating system to perform. On most Linux systems, the shell program is called BASH (The Bourne Again Shell), but there are others, including the C Shell (tcsh) and the KORN shell (ksh).

How to Access the Linux Shell

Generally, as a user, you interact with the shell program through a terminal emulation program such as XTerm, konsole, or gnome-terminal.

If you run a windows manager such as Openbox or a desktop environment such as GNOME or KDE, you'll find a terminal emulator either from a menu or a dash. On many systems, the shortcut CTRL+ALT+T opens a terminal window.

Alternatively, you can switch to another TTY (teletypewriter), which provides direct access to a command-line shell. You can do this by pressing CTRL+ALT+F1 or CTRL+ALT+F2.

What Is a Shell Level?

When you run a command in a shell, it runs at the shell level. Within a shell, you can open another shell, which makes it a subshell of the shell that opened it. Therefore, the parent shell is considered the level 1 shell, and the child shell is a level 2 shell.

How to Display the Shell Level

The way to tell which shell level you are running in is to use the $SHLVL variable. To see the shell level that you are currently running in, type the following:

echo $SHLVL

When you run the above command in a terminal window, the result returned is 2. If, however, you run the same command using the tty, then the result is 1. The tty doesn't run a desktop environment and is a level 1 shell.

Why is this the case? The desktop environment you are running is run on top of a shell. That shell is level 1. Any terminal window you open from that desktop environment is a child of the shell that opened the desktop environment. Therefore, the shell level cannot start at any number other than 2.

How to Create Subshells

The easiest way to test the concept of shells and subshells is as follows. Open a terminal window, then type the following:

echo $SHLVL

When you run this command from a terminal window, the minimum shell level is 2.

Type the following in the terminal window:


The sh command runs an interactive shell. This means you are using a shell within a shell or a subshell.

If you type this again:

echo $SHLVL

You see that the shell level is set to 3. Running the sh command from the subshell opens a subshell of the subshell, and so the shell level is at level 4.

Why Is Shell Level Important?

The shell level is important when thinking about the scope of variables within your scripts. For a simple example:

echo $dog

If you run the above command in a shell, the word maisie displays to the terminal window.

To open a new shell, type the following:


When you run this command, you'll see that nothing is returned:

echo $dog

That's because the $dog variable is only available at shell level 2. If you type exit to exit the subshell and run echo $dog again, the word maisie displays again.

It is also worth thinking about the behavior of global variables within a shell.

Open a new terminal window, then type the following:

export dog=maisie
echo $dog

As expected, the word maisie displays. Next, open a subshell and type echo $dog again. This time, the word maisie displays even though you are in a subshell. The reason for this is that the export command made the $dog variable global. Changing the $dog variable in the subshell, even if you use the export command, doesn't affect its parent shells.

Knowing the shell level you're working in has significance when writing scripts. The examples in this article are simple, but it's common for one shell script to call another shell script which in turn calls another shell script. All of these run at different levels. Knowing the shell level is important.

Was this page helpful?