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条回答
做个烂人
2楼-- · 2019-03-08 07:51

Here is a simplified version of part of my prompt:

PS1='$(((SHLVL>1))&&echo $SHLVL)\$ '

If I'm not in a subshell, it doesn't add anything extra, but it shows the depth if I'm in any level of subshell.

查看更多
登录 后发表回答