cgp Asked: 2009-06-16 10:42:13 +0800 CST2009-06-16 10:42:13 +0800 CST 2009-06-16 10:42:13 +0800 CST How to determine how many shells deep I might be? 772 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. scripting bash 4 Answers Voted Best Answer brian-brazil 2009-06-16T10:46:40+08:002009-06-16T10:46:40+08:00 echo $SHLVL From the bash manpage: SHLVL Incremented by one each time an instance of bash is started. rkthkr 2009-06-16T10:50:00+08:002009-06-16T10:50:00+08:00 One way is to use pstree: $ pstree -h [...] ├─sshd─┬─sshd───sshd───bash───bash───bash───bash───bash───pstree │ └─sshd───sshd───bash───bash───bash───bash [...] Kevin M 2009-06-16T10:48:07+08:002009-06-16T10:48:07+08:00 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 Dennis Williamson 2009-06-17T11:40:58+08:002009-06-17T11:40:58+08:00 Also useful: $BASH_SUBSHELL $ echo $SHLVL 1 $ (echo $SHLVL) 1 $ echo $BASH_SUBSHELL 0 $ (echo $BASH_SUBSHELL) 1 $ ( (echo $BASH_SUBSHELL) ) 2
From the bash manpage:
One way is to use pstree:
This will catch if you do something like:
Although that won't catch something like if you SSH into localhost.
Also useful:
$BASH_SUBSHELL