$ SHLVL does not increase (echo $ SHLVL)

I study shell scripts and loop into this small experimental shell script:

#!/usr/bin/env bash
A=1
(A=2;echo "A is $A in $SHLVL")
echo "A is $A in $SHLVL"
exit 0

Without a doubt, $Ain line 3 and line 4 they are different from each other, which can be explained by the fact that this is because the parent process cannot read the variables created in the child process, that is, the subshell. However, $SHLVLon line 3 and line 4 are the MOST that I thought there $Ashould be more in line 3 than $Ain line 4 by 1. Have the commands in line 3 in the sub-sequence been executed? I do not know where I misinterpreted.

+4
source share
2 answers

You really need to use $BASH_SUBSHELLinstead $SHLVLhere.

$BASH_SUBSHELL - , .

script :

#!/usr/bin/env bash

A=1
(A=2;echo "A is $A in $BASH_SUBSHELL:$SHLVL")
echo "A is $A in $BASH_SUBSHELL:$SHLVL"

:

A is 2 in 1:2
A is 1 in 0:2

$BASH_SUBSHELL , $SHLVL .

+3

script (), , . $SHLVL , , $BASH_SUBSHELL .

$SHLVL , . ssh , 1. script, 2. () , $SHLVL .

$BASH_SUBSHELL , - . , .

$ cat shlvl.sh 
#!/usr/bin/env bash
A=1
(A=2;echo "A is $A in $SHLVL" and  bash_subshell is $BASH_SUBSHELL)
echo "A is $A in $SHLVL ans bash_subshell is  $BASH_SUBSHELL"
exit 0
$ ./shlvl.sh 
A is 2 in 3 and bash_subshell is 1
A is 1 in 3 ans bash_subshell is  0
0

Source: https://habr.com/ru/post/1692095/


All Articles