How And Why You Would Use The $SHLVL Variable

How To Use $SHLVL
How To Use $SHLVL.

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

What Is A Shell?

A shell takes commands and gives them 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 available 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 the use of a terminal emulation program such as XTerm, konsole or gnome-terminal.

If you are running a windows manager such as Openbox or a desktop environment such as GNOME or KDE you will find a terminal emulator either from a menu or a dash. On many systems the shortcut CTRL ALT and T will open a terminal window as well.

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 and F1 or CTRL ALT and F2 etc.

What Is A Shell Level

When you run a command in a shell it runs at something called the shell level. Within a shell you can open another shell which makes it a subshell or the shell that opened it.

Therefore the parent shell would be considered perhaps the level 1 shell and the child shell would be a level 2 shell.

How To Display The Shell Level

It should come as no surprise based on the title of the article that the way you can tell which shell level you are running in is by using the $SHLVL variable.

To see the shell level that you are currently running in type the following:

echo $SHLVL

Rather interestingly if you run the above command within a terminal window you might be surprised to see that the result returned is 2.

If however you run the same command using the tty then the result is 1.

Why is this the case you might ask? Well the desktop environment you are running is being run on top of a shell. That shell would be level 1. Any terminal window you open from within that desktop environment has to be a child of the shell that opened the desktop environment and therefore the shell level cannot start at any number other than 2.

The tty isn't running a desktop environment and is therefore simply a level 1 shell.

How To Create Subshells

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

echo $SHLVL

As we know from a terminal window the minimum shell level is 2.

Now within the terminal window type the following:


The sh command on its own runs an interactive shell which means you are using a shell within a shell or a subshell.

If you now type this again:

echo $SHLVL

You will see that the shell level is set to 3. Running the sh command from within the subshell will open a subshell of the subshell and so the shell level will be at level 4.

Why Is The Shell Level Important?

The shell level is important when thinking about the scope of variables within your scripts.

Let's start with something simple:

echo $dog

If you run the above command in a shell the word maisie will be displayed to the terminal window.

Open a new shell by typing the following:


If you run this command you will see that nothing is actually returned:

echo $dog

That is 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 will be displayed again.

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

Start off in a new terminal window and type the following:

export dog=maisie
echo $dog

As you would expect the word maisie is displayed. Now open a subshell and type echo $dog again. This time you will see that the word maisie is displayed 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 within the subshell even if you use the export command has no affect on its parent shells.

Hopefully from this you can see that knowing the shell level you are working in has some significance when writing scripts.

The examples I have given are very simplistic but it is quite common for one shell script to call another shell script which in turn calls another shell script all of them now running at different levels. Knowing the shell level can be very important.

More From Us