The answer is in this special shell variable "$?". In this variable is saved exit status of last command that ended in background.
In next example "paranormal_directory" doesn't exists, at all. It is paranormal directory :-) In this example, $? variable will be 2, because command ls fails:
ls paranormal_directory 1>/dev/null 2>&1 echo $?
Listing home folder is always safe. Executing ls without any parameter lists home folder of current user. In this example, ls will succeed, so variable "$?" will be 0:
ls 1>/dev/null 2>&1 echo $?
But be careful, if you read this variable two times (echo $?). In next example, "echo $?" (on line 3) will show you the output of the first echo command (on line 2):
ls paranormal_directory 1>/dev/null 2>&1 echo $? echo $?
Output: 2 0
What happens if you don't specify exit code in script? When the exit code is not specified with the exit command, the exit code of script will be the exit code of last executed command.
Was this information helpful to you? You have the power to keep it alive.
Each donated € will be spent on running and expanding this page about UNIX Shell.
We prepared for you video course Marian's BASH Video Training: Mastering Unix Shell, if you would like to get much more information.
Thank you. Marian Knezek