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.

4 thoughts on “check last exit status code in bash shell

  1. Pete

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

    Reply
  2. Paul

    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.

    Reply

Leave a Reply

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