When the SHLVL variable in Bash is empty

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.