Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

4 Answers 4

up vote 18 down vote accepted
echo $SHLVL

From the bash manpage:

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

share|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

One way is to use pstree:

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

Also useful: $BASH_SUBSHELL

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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