Linux bash exit status and how to set exit status in bash


#1

Originally published at: https://www.cyberciti.biz/faq/linux-bash-exit-status-set-exit-statusin-bash/

Can you explain bash exit status code? How do I set bash exit status in my Linux shell scripts?


#2

Very simple. Use the command exit.
This is an example:

#!/bin/bash
if (( $1 == 1 )) ; then
exit 10
else
exit 20
fi

If you run the program with a paramenter, when it is 1, exit code is 10, when any other number, exit code is 20.
You can check running the script and then typing:

echo $?