Determine Subshell Level

Sep 8, 2020 • Matt Karmazyn
Edited: Sep 16, 2020


How to determine the level of subshell you are in.

Lately I have been using aws-vault to manage my session across multiple AWS accounts. This tool puts you in a subshell, but after some time I forget if this particular terminal session is a subshell or not. The $SHLVL variable tracks your shell nesting level.

$ echo $SHLVL
1
$ bash
$ echo $SHLVL
2
$ exit
$ echo $SHLVL
1

I now include this in my $PS1 prompt for ease of use.