How to know the "level" of shells I am in?

All we need is an easy explanation of the problem, so here it is.

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 solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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 (~/.xinitrc or ~/.xsession).

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 SHLVL.

Method 2

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.

Method 3

Not very elegant, but you could use tree view in htop to see parent-child relationship of your shell to other running proccesses.

$ htop # then press 't' and scroll around or 'Ctrl-W' to find your shell proccess

And use that to deduce the ammount of shells you will need to exit, before ariving at the “root” shell

Method 4

You can use the environment variable SHLVL:

echo "$SHLVL"

Method 5

I find that

$ pstree -u | grep ${USER}

can be quite helpful for seeing where I “fit in” on a system. I prefer it over pstree -u ${USER}.

Occasionally it will remind me that I left a nohup job running, or similar.

(Also, I use alias grep='grep --color=auto'.)

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply