How do I know if I'm running a nested shell?

2019-03-08 06:50发布

When using a *nix shell (usually bash), I often spawn a sub-shell with which I can take care of a small task (usually in another directory), then exit out of to resume the session of the parent shell.

Once in a while, I'll lose track of whether I'm running a nested shell, or in my top-level shell, and I'll accidentally spawn an additional sub-shell or exit out of the top-level shell by mistake.

Is there a simple way to determine whether I'm running in a nested shell? Or am I going about my problem (by spawning sub-shells) in a completely wrong way?

7条回答
你好瞎i
2楼-- · 2019-03-08 07:27

pstree -s $$ is quite useful to see your depth.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-03-08 07:27

ptree $$ will also show you how many levels deep you are

查看更多
看我几分像从前
4楼-- · 2019-03-08 07:33

The environment variable $SHLVL contains the shell "depth".

echo $SHLVL

The shell depth can also be determined using pstree (version 23 and above):

pstree -s $$ | grep sh- -o | wc -l

I've found the second way to be more robust than the first whose value was reset when using sudo or became unreliable with env -i.

None of them can correctly deal with su.


The information can be made available in your prompt:

PS1='\u@\h/${SHLVL} \w \$ '
PS1='\u@\h/$(pstree -s $$ | grep sh- -o | tail +2 | wc -l) \w \$ '

The | tail +2 is there to remove one line from the grep output. Since we are using a pipeline inside a "$(...)" command substitution, the shell needs to invoke a sub-shell, so pstree report it and grep detects one more sh- level.


In debian-based distributions, pstree is part of the package psmisc. It might not be installed by default on non-desktop distributions.

查看更多
The star\"
5楼-- · 2019-03-08 07:42

If you running inside sub-shell following code will yield 2:

ps | fgrep bash | wc -l

Otherwise, it will yield 1.

EDIT Ok, it's not so robust approach as was pointed out in comments :)
Another thing to try is

ps -ef | awk '{print $2, " ", $8;}' | fgrep $PPID 

will yield 'bash' if you in sub-shell.

查看更多
女痞
6楼-- · 2019-03-08 07:45

The $SHLVL variable tracks your shell nesting level:

$ echo $SHLVL
1
$ bash
$ echo $SHLVL
2
$ exit
$ echo $SHLVL
1

As an alternative to spawning sub-shells you could push and pop directories from the stack and stay in the same shell:

[root@localhost /old/dir]# pushd /new/dir
/new/dir /old/dir
[root@localhost /new/dir]# popd
/old/dir
[root@localhost /old/dir]#
查看更多
你好瞎i
7楼-- · 2019-03-08 07:45

Look at $0: if it starts with a minus -, you're in the login shell.

查看更多
登录 后发表回答