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.

link|improve this question

feedback

4 Answers

up vote 18 down vote accepted
echo $SHLVL

From the bash manpage:

SHLVL Incremented by one each time an instance of bash is started.

link|improve this answer
Ah, there it is! – altCognito Jun 15 '09 at 18:47
2  
So I guess checking the parent process ID recursively would be a waste of time, eh? – sangretu Jun 15 '09 at 18:50
1  
Checking the parent process does have the advantage that it'll work for any shell. – user8996 Jun 15 '09 at 18:52
feedback

One way is to use pstree:

$ pstree -h
[...]
├─sshd─┬─sshd───sshd───bash───bash───bash───bash───bash───pstree
│      └─sshd───sshd───bash───bash───bash───bash
[...]
link|improve this answer
feedback
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
link|improve this answer
feedback

Also useful: $BASH_SUBSHELL

$ echo $SHLVL
1
$ (echo $SHLVL)
1
$ echo $BASH_SUBSHELL
0
$ (echo $BASH_SUBSHELL)
1
$ ( (echo $BASH_SUBSHELL) )
2
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.