Bash: read a command's exit status with $?
Use $? to get the exit status of the last command.
In the shell, every command returns an exit status: 0 means success, any other value signals an error. The special variable $? holds the exit status of the last executed command.
date &> /dev/null
echo $?
The first line runs date, sending standard output and errors to /dev/null; the second prints its exit status (0 here). For more details, see Bash exit.
| Variable | Meaning |
|---|---|
$? |
Exit status of the last executed command |
$# |
Number of arguments passed to the script or function |