diff --git a/Shell/GlobalState.h b/Shell/GlobalState.h index 1ca4a981e3..d87d798356 100644 --- a/Shell/GlobalState.h +++ b/Shell/GlobalState.h @@ -14,6 +14,7 @@ struct GlobalState { struct termios termios; bool was_interrupted { false }; bool was_resized { false }; + int last_return_code { 0 }; }; extern GlobalState g; diff --git a/Shell/main.cpp b/Shell/main.cpp index 24d66ff38f..1ee0003b55 100644 --- a/Shell/main.cpp +++ b/Shell/main.cpp @@ -307,6 +307,8 @@ static Vector expand_parameters(const StringView& param) return { param }; String variable_name = String(param.substring_view(1, param.length() - 1)); + if (variable_name == "?") + return { String::number(g.last_return_code) }; char* env_value = getenv(variable_name.characters()); if (env_value == nullptr) @@ -546,6 +548,8 @@ static int run_command(const String& cmd) } } + g.last_return_code = return_value; + // FIXME: Should I really have to tcsetpgrp() after my child has exited? // Is the terminal controlling pgrp really still the PGID of the dead process? tcsetpgrp(0, getpid());