get last exit code in bash

· bergpb's blog

How to get the last exit code in a bash command or script

Everytime that a bash command is performed, a status code is returned indicating if the command runs with or without errors.

By convention the code 0 means that command was executed successfully, any other exit code will indicate an error on command execution.

To verify the last status code after run a command or a bash script, you can use $?:

# example
> date
Mon Jul 28 10:33:34 PM -03 2025

> $?
0
last updated: