bash - Preserve exit code when using '$PROMPT_COMMAND' -
i have next code (simplified clarity) called $prompt_command after every command:
function previous_command_status() { exit_code=$?; if [ $exit_code -eq 0 ]; echo "command successful" else echo "command failed exit code $exit_code" fi } the problem is, seems [ $exit_code -eq 0 ] part changing exit code, unable utilize or store exit code after command has finished running. example:
$ ./failing_script.sh command failed exit code 255 $ echo $?; 1 # exit code of 'if' statement, not of 'bad' i cannot "pass value along", because if add together line exit $exit_code within function, terminal window closes immediately.
is there way me "preserve" exit code of previous command, or run set of commands in such way won't modify exit value?
you can't preserve it. if utilize case statements echo still alter it. can set return:
exit_code=$?; if [ $exit_code -eq 0 ]; echo "command successful" else echo "command failed exit code $exit_code" fi homecoming "$exit_code" you can utilize global variable store code however.
bash shell exit-code
No comments:
Post a Comment