Bash Exit Status
Every command returns an exit status when it ends: a number from 0 to 255.
0 means success, any other number means something went wrong. The special variable $? holds the exit status of the last command.
Read the exit status
ls notes.txt
notes.txt
echo $?
0
ls missing.txt
ls: cannot access 'missing.txt': No such file or directory
echo $?
2
Set the exit status in a script
Use exit with a number to end a script with that status. Save this as check.sh:
#!/bin/bash
echo "Checking..."
exit 3
Run it:
./check.sh; echo "Exit status: $?"
Checking...
Exit status: 3