variable

Bash SHLVL — shell nesting level

Normally incremented when a new Bash instance starts. A parenthesized subshell is not equivalent to starting another Bash executable. The example compares one child Bash with its parent; unusual inherited values may be normalized. Run in a fresh private directory with HOME set to that directory and LC_ALL=C. Expected output (one item per line): child incremented.

Formal Syntax & Structure

bash syntaxbash
${SHLVL}

Return Value

integer

Current Bash invocation nesting level.

Working Example

Example Snippetbash
parent=$SHLVLbash --noprofile --norc -c 'printf "%s\n" "$SHLVL"' > child.level || exit 1IFS= read -r child < child.level || exit 1(( child == parent + 1 )) || exit 1printf 'child incremented\n'
Bash Reference