3

This question already has an answer here:

The bash manual says

BASH_SUBSHELL Incremented by one within each subshell or subshell environment when the shell begins executing in that environment. The initial value is 0.

SHLVL Incremented by one each time a new instance of Bash is started. This is intended to be a count of how deeply your Bash shells are nested.

What are the differences between the two builtin variables?

Specifically what differences are between "subshell or subshell environment" and "Bash shells"?

In the following example, why does the value of BASH_SUBSHELL not change, while the value of SHLVL changes?

$ echo $BASH_SUBSHELL
0
$ echo $SHLVL
1
$ bash
$ echo $BASH_SUBSHELL
0
$ echo $SHLVL
2

marked as duplicate by Gilles bash Dec 10 '16 at 22:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

this refer to subshell within main shell

compare

echo "a" $BASH_SUBSHELL "b" $SHLVL

with

( echo "a" $BASH_SUBSHELL "b" $SHLVL )

or

( ( echo "a" $BASH_SUBSHELL "b" $SHLVL ) ) 

this might be useful inside function if you set env var.

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