Here is the video version, if you prefer it:
Exit codes answer the question above – How did it go? That is, exit codes tell you if the shell script finished successfully or has it encountered some problems and it didn’t finish successfully.
Exit code is stored in a special variable $?
. Let’s look at that variable:
mislav@mislavovo-racunalo:~/Linux_folder$ ls
1264.txt aba2.txt ab-hard a-symbolic file.txt
1-4.txt~ aba2.txt~ ab-new.txt a.txt~ tutorialScript.sh
2345.txt aba.txt ab.txt~ cb.txt tutorialScript.sh~
ab2.txt aba.txt~ a-new.txt cb.txt~
mislav@mislavovo-racunalo:~/Linux_folder$ echo $?
0
mislav@mislavovo-racunalo:~/Linux_folder$ ls allla
ls: cannot access 'allla': No such file or directory
mislav@mislavovo-racunalo:~/Linux_folder$ echo $?
2
If a command executes successfully, its exit code is 0. Anything other than 0 indicates a failure of some sort. To find out what exactly went wrong, you can look at EXIT VALUE or DIAGNOSTICS section in the man pages of the command that failed. (Ward, 2014)
Hope you learned something new!
Subscribe to my newsletter to keep abreast of the interesting things I'm doing. I will send you the newsletter only when there is something interesting. This means 0% spam, 100% interesting content.