diff --git a/crates/nu-command/src/system/exec.rs b/crates/nu-command/src/system/exec.rs index 8a2b24a113..e9f49b86c1 100644 --- a/crates/nu-command/src/system/exec.rs +++ b/crates/nu-command/src/system/exec.rs @@ -61,6 +61,16 @@ On Windows based systems, Nushell will wait for the command to finish and then e let envs = env_to_strings(engine_state, stack)?; command.env_clear(); command.envs(envs); + // Decrement SHLVL as removing the current shell from the stack + // (only works in interactive mode, same as initialization) + if engine_state.is_interactive { + if let Some(shlvl) = engine_state.get_env_var("SHLVL") { + let shlvl = shlvl.as_int().unwrap_or(1) - 1; + command.env("SHLVL", shlvl.to_string()); + } else { + command.env("SHLVL", "0"); + } + } // Configure args. let args = crate::eval_arguments_from_call(engine_state, stack, call)?; diff --git a/src/main.rs b/src/main.rs index 7d39aef02b..db55db3ff2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -325,7 +325,7 @@ fn main() -> Result<()> { if engine_state.is_interactive { let mut shlvl = engine_state .get_env_var("SHLVL") - .map(|x| x.as_str().unwrap_or("0").parse::().unwrap_or(0)) + .map(|x| x.as_int().unwrap_or(0)) .unwrap_or(0); shlvl += 1; engine_state.add_env_var("SHLVL".to_string(), Value::int(shlvl, Span::unknown()));