Finding the shell level with bash


I occassionally need to start bash more than once, and typically use exit (my .bash_profile is set to ignore ^D (EOF)) to leave the new version of bash. Periodically I hit exit one too many times, which causes my terminal session (and debugging sessions) to dissappear. Since this caused me a good deal of grief yesterday, I decided to find a fix in the bash documentation. After reading for 30-minutes or so, I came across the bash SHLVL variable:

$ echo SHLVL
1

$ bash

$ echo SHLVL
2

$ bash

$ echo SHLVL
3

$ exit

$ echo SHLVL
2

This is a useful variable, and I plan to make a “shellami” alias. Tight!

This article was posted by Matty on 2006-02-11 13:27:00 -0400 -0400