I am confused between the two.
While the $BASH_SUBSHELL internal variable indicates the nesting level of a subshell, the $SHLVL variable shows no change within a subshell.
What does it exactly mean? If i open a shell within another shell, the value of $SHLVL increments. Isn't that a subshell?
$SHLVL:
Shell level, how deeply Bash is nested. If, at the command-line, $SHLVL is 1, then in a script it will increment to 2. This variable is not affected by subshells.
$BASH_SUBSHELL:
A variable indicating the subshell level.
Here is how they will have different values:
No, manually running a new shell (via
/bin/sh
or/bin/bash
etc.) is not a subshell in this context.A subshell is when the shell spawns a new shell instance on its own to handle some work.
Using Command Substitution (i.e.
$(command)
) is a subshell (as is the older backticks invocation).Using a pipeline (i.e.
echo '5.1+5.3' | bc -l
) creates subshells for each component of the pipeline.Using Process Substitution (i.e.
<(command)
) creates a subshell.Grouping commands (i.e.
(declare a=5; echo $a)
) creates a subshell.Running commands in the background (i.e.
sleep 1 &
) creates a subshell.There may be other things as well but those are the common cases.
Testing this is easy:
The source of your quote (the generally poor, and often better avoided, ABS) even demonstrates this a little bit (and in a rather unclear manner, just another instance of the general lack of rigor and quality in that "Advanced" guide):