The SHLVL environment variable represents the depth of Shell nesting execution.
|
$ echo $SHLVL
1
$ bash
$ echo $SHLVL
2
$ bash
$ echo $SHLVL
3 |
In Bash, the range for this value is [1, 1000], which resets to 1 at 1001.
|
$ shlvl=1000
$ bash
Bash:warning:shell level (1001) too high, resetting to 1
$ echo $SHLVL
1 |
I thought that was the case, but I found an exception:
|
$ shlvl=999
$ bash
$ echo $SHLVL
|
When nesting 1000 layers, SHEVL will be an empty string, meaning that SHEVL actual values will be polled in [1, 999] and empty 1000 values, and will not actually reach 1000.
See the next Bash Source:
voidadjust_shell_level (change)intChange ; { Charnew_level[5], *old_shlvl; intmax_t Old_level; Shell_var*Temp_var; OLD_SHLVL= Get_string_value ("SHLVL"); if(OLD_SHLVL = =0|| *OLD_SHLVL = =' /'|| Legal_number (OLD_SHLVL, &old_level) = =0) Old_level=0; Shell_level= Old_level +Change ; if(Shell_level <0) Shell_level=0; Else if(Shell_level > +) {internal_warning (_ ("shell level (%d) too high, resetting to 1"), shell_level); Shell_level=1; } /*We don ' t need the full generality of itos here.*/ if(Shell_level <Ten) {new_level[0] = Shell_level +'0'; new_level[1] =' /'; } Else if(Shell_level < -) {new_level[0] = (Shell_level/Ten) +'0'; new_level[1] = (shell_level%Ten) +'0'; new_level[2] =' /'; } Else if(Shell_level < +) {new_level[0] = (Shell_level/ -) +'0'; Old_level= shell_level% -; new_level[1] = (Old_level/Ten) +'0'; new_level[2] = (old_level%Ten) +'0'; new_level[3] =' /'; } Temp_var= Bind_variable ("SHLVL", New_level,0); Set_auto_export (Temp_var);}Static voidInitialize_shell_level () {Adjust_shell_level (1);}
It writes the logic of the number to the string only to Shell_level < 1000, it looks like the last other else, whether or not the author intends, this performance should not change, anyway this is the edge case, by the way, Ksh and zsh There is no such 1000 limit.
When the SHLVL variable in Bash is empty