check last exit status code in bash shell

In case you’re curious (while debugging a program or a script) about the exit status code returned by the last shell command you ran, the incantation to retrieve it in the bash shell is:

echo $?

Given the nature of this variable (no one indexes text like ‘$?’), it’s annoyingly hard to Google for.

5 Replies to “check last exit status code in bash shell”

  1. Thanks for this! I am porting shell code from OpenVMS to Unix, and was searching for the equivalent to DCL’s $status.

  2. Hey, if you’re trying to help people searching for “$?”, you should tag the post with the phrase(s) people might search for. Bash dollar sign question mark!

    And thanks by the way, this is what I was looking for.

Leave a Reply to Leo Cancel reply

Your email address will not be published. Required fields are marked *