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?
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 $?
1 Like
How does one clear the status code without exiting, for future checks in the same script? I can use $? to echo/compare the status code of the last command, but I cannot use β$?=0β to clear the value. There must be a better way than running a dummy command before checking $?. Thx
You canβt as $?
can be modified by shell only. Try to set a new value but that txit the shell too. For instance:
exit 0