I'm wondering whether I've called the shell recursively, is there an easy way to find out? Is any solution specific to the shell? I'm using bash.
4 Answers
echo $SHLVL
From the bash manpage:
SHLVL Incremented by one each time an instance of bash is started.
-
-
2So I guess checking the parent process ID recursively would be a waste of time, eh?– sangretuJun 15, 2009 at 18:50
-
1Checking the parent process does have the advantage that it'll work for any shell. Jun 15, 2009 at 18:52
One way is to use pstree:
$ pstree -h
[...]
├─sshd─┬─sshd───sshd───bash───bash───bash───bash───bash───pstree
│ └─sshd───sshd───bash───bash───bash───bash
[...]
echo $SHLVL
This will catch if you do something like:
[sharpestmarble@sandbox ~]$ bash
Although that won't catch something like if you SSH into localhost.
[sharpestmarble@sandbox ~]$ ssh localhost
Also useful: $BASH_SUBSHELL
$ echo $SHLVL
1
$ (echo $SHLVL)
1
$ echo $BASH_SUBSHELL
0
$ (echo $BASH_SUBSHELL)
1
$ ( (echo $BASH_SUBSHELL) )
2