Bash tips & tricks [ep. 6]: Check the exit code

This is the sixth epidose of a small series.


Every command in a script may fail due to external reasons. Bash programming is not functional programming! 🙂

After running a command, make sure that you check the exit code and either raise a warning or exit with an error, depending on how a failure can impact the execution of the script.


The worst example is not to check the exit code at all:

Next one is better, but you may have a lot of additional code to type:

Again, Log_Close, eok, eerror, etc are functions defined using the previous Bash Tips & Tricks in this series.


Define once the check functions that you will use after every command:


The following two tabs change content below.


Oracle ACE Director and Principal Consultant at Trivadis
Ludovico is an Oracle ACE Director, frequent speaker and community contributor, working as Principal Consultant for Trivadis, the leading Oracle consulting firm in Switzerland and German-speaking Europe.

3 thoughts on “Bash tips & tricks [ep. 6]: Check the exit code

  1. Pingback: Bash tips & tricks [ep. 6]: Check the exit code - Ludovico Caldara - Blogs - triBLOG

  2. Thanks for the tips, could You explain why there is a shift in function ?

  3. Wow, I realize now that I have never rplied to your comment… Sorry Grzegorz.
    I use shift get the rid of the first parameter, so that after that I can refer to $@ without caring about hte first parameter that was the exit code.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.