If I open a terminal like xterm I will have a shell. Then if I use ssh or zsh I will have another level of shell. Is there a way to know how many times I have to Ctrl+D or type exit to exit all of them? My real intention is to exit everything except the root shell.It will also be nice to know what effect(s) terminal multiplexers (like screen) have on the solution.PS: Please feel free to change the title, I don't know if those are the correct terms.
How to know the level of shells I am in?
shell;terminal
One (admittedly not perfect) way is to add the result of ps --no-headers -o comm $PPID to your shell prompt. This will tell you the name of the process that spawned your shell. If you're inside a nested zsh, it will say zsh. If you're inside a top-level zsh, it will read xterm or screen or whatever your shell is running in.To tell whether you're inside an ssh or su session, you can simply look at the hostname and username part of your prompt.When you're running nested screens (which I don't imagine is a common situation), there's no way I can think of to tell whether you're in the top-level shell of a nested screen, or the top-level shell of the top-level screen. You could configure screen to always display a status line, which would cause multiple status lines to be displayed, if you're in nested screens.