As @ John Kugelman says echo $SHLVL will tell you the depth of the bash shell.
And, as @Dennis Williamson shows, you can edit your invitation via the PS1 variable to print this value.
I prefer it to always print the shell depth value, so here's what I did: edit the file "~ / .bashrc":
gedit ~/.bashrc
and add the following line to the end:
export PS1='\$SHLVL'":$SHLVL\n$PS1"
Now you will always see a listing of your current bash level just above your prompt. Example: here you can see that I am at bash level (depth) 2, as indicated by $SHLVL:2 :
$ SHLVL: 2
7510-gabriels ~ $
Now follow the prompt when I go down to some bash levels with the bash command and then return via exit . Here you see my commands and a hint (answer), starting from level 2 and dropping to 5, then returning to level 2:
$SHLVL:2 7510-gabriels ~ $ bash $SHLVL:3 7510-gabriels ~ $ bash $SHLVL:4 7510-gabriels ~ $ bash $SHLVL:5 7510-gabriels ~ $ exit exit $SHLVL:4 7510-gabriels ~ $ exit exit $SHLVL:3 7510-gabriels ~ $ exit exit $SHLVL:2 7510-gabriels ~ $
Bonus: show your current git branch , which you are also in!
Your invitation will also display your git branch that you are working on ; instead, use the following in the ~ / .bashrc file:
git_show_branch() { __gsb_BRANCH=$(git symbolic-ref -q --short HEAD 2>/dev/null) if [ -n "$__gsb_BRANCH" ]; then echo "$__gsb_BRANCH" fi } export PS1="\e[7m\$(git_show_branch)\e[m\n\h \w $ " export PS1='\$SHLVL'":$SHLVL $PS1"
Source: I have no idea where git_show_branch() came from, but I got it from Jason McMullan on April 5, 2018. Then I added the $SHLVL shown above only last week.
Output Example:
$ SHLVL: 2 master
7510-gabriels ~ / GS / dev / temp $
And here is a screenshot showing it in all its glory. Pay attention to the name of the git, master branch, highlighted in white!

Gabriel Staples Aug 26 '19 at 23:12 2019-08-26 23:12
source share