4

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
Tim
  • 101,790

1 Answers1

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.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Archemar
  • 31,554