On OS X, Linux, and other systems, you can use $? to get the exit code of the last command. Is there any way to exit codes of older commands? Either by saying "give me the exit code of the nth previous command" or by saying "give me the exit code of the process with PID p"?
Asked
Active
Viewed 5,183 times
5
Amit Kumar Gupta
- 233
1 Answers
7
The exit code of a command is output only once, and if not processed further the only thing that saves that exit code is the shell. bash only saves the exit code for the last command.
In order to look up the exit code of older commands you need to save them to a variable, like so:
$ echo "This command will succeed"
$ exitcode=$?
...
$ echo $exitcode
0
Arkenklo
- 391