Linux Tutorial Series

Linux Tutorial Series – 184 – How did it go – exit codes

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~

2345.txt aba.txt ab.txt~ cb.txt

ab2.txt aba.txt~ a-new.txt cb.txt~

mislav@mislavovo-racunalo:~/Linux_folder$ echo $?


mislav@mislavovo-racunalo:~/Linux_folder$ ls allla

ls: cannot access 'allla': No such file or directory

mislav@mislavovo-racunalo:~/Linux_folder$ echo $?


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!