简体   繁体   中英

Zsh weirdness regarding $SHLVL

Can somebody explain the following behaviour, please?

$ echo $SHLVL
1
$ zsh -c 'echo $SHLVL'
2
$ zsh -c '(echo $SHLVL)'
1

I don't understand last result. Does zsh reset $SHLVL for () subshells? Bash result is different:

$ echo $SHLVL
1
$ bash -c 'echo $SHLVL'
2
$ bash -c '(echo $SHLVL)'
2

Which, at least, seems more logical. In an case, I was expecting 3 for the last result when I tested this. Why no shell gives that result? What I am misunderstanding about $SHLVL?

In bash the variable SHLVL is not incremented for subshells, see abs guide . For nested subshells see BASH_SUBSHELL .

In zsh the doc says that SHLVL counts the number of shells, see here , and that, surprise, ZSH_SUBSHELL counts nested subshells, see here .

To me, the decrementing effect of SHLVL in a zsh subshell is a bug; another possibility could be that SHLVL and ZSH_SUBSHELL might be added (an undocumented feature?).

$ zsh -c 'print $SHLVL $ZSH_SUBSHELL ; (print $SHLVL $ZSH_SUBSHELL)'
2 0
1 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM