diff --git a/src/repl.rs b/src/repl.rs index 5739e7ec51..909a411fc4 100644 --- a/src/repl.rs +++ b/src/repl.rs @@ -86,7 +86,7 @@ pub(crate) fn evaluate( let exceptions = crate::utils::external_exceptions(engine_state, &stack); engine_state.external_exceptions = exceptions; - // seed the cmd_duration_ms env var + // seed env vars stack.add_env_var( "CMD_DURATION_MS".into(), Value::String { @@ -95,6 +95,14 @@ pub(crate) fn evaluate( }, ); + stack.add_env_var( + "LAST_EXIT_CODE".into(), + Value::Int { + val: 0, + span: Span { start: 0, end: 0 }, + }, + ); + loop { if is_perf_true() { info!( diff --git a/src/utils.rs b/src/utils.rs index 31f5b6f495..2b481e2b28 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -227,7 +227,13 @@ pub(crate) fn eval_source( } match eval_block(engine_state, stack, &block, input, false, false) { - Ok(pipeline_data) => { + Ok(mut pipeline_data) => { + if let PipelineData::ExternalStream { exit_code, .. } = &mut pipeline_data { + if let Some(exit_code) = exit_code.take().and_then(|it| it.last()) { + stack.add_env_var("LAST_EXIT_CODE".to_string(), exit_code); + } + } + if let Err(err) = print_pipeline_data(pipeline_data, engine_state, stack) { let working_set = StateWorkingSet::new(engine_state);