You have in fact hit upon the correct term¹. There is an environment variable
SHLVL which all major interactive shells (bash, tcsh, zsh) increment by 1 when they start. So if you start a shell inside a shell,
SHLVL increases by 1.
This doesn't directly answer your concern, however, because
SHLVL carries over things like terminal emulators. For example, in my typical configuration,
$SHLVL is 2 in an xterm, because level 1 corresponds to the shell that runs my X session (
What I do is to display
$SHLVL in my prompt, but only if the parent process of the shell is another shell (with heuristics like “if its name ends in
sh plus optional punctuation and digits, it's a shell”). That way, I have an obvious visual indication in the uncommon case of a shell running under another shell.
Maybe you would prefer to detect shells that are running directly under a terminal emulator.
You can do this fairly accurately: these are the shells whose parent process has a different controlling terminal, so that
ps -o tty= -p$$ and
ps -o tty= -p$PPID produce different output. You might manually reset
SHLVL to 1 in these shells, or set your own
TERMSHLVL to 1 in these shells (and incremented otherwise).
Although one wouldn't think it looking at the manual pages: none of the three shells that support it include the word “level” in their documentation of