errexit#

The idea behind this is that the script should exit whenever it encounters an error, also known as a non–zero exit code. This script illustrates the idea:

Running this script is completely safe! The false command returns a non–zero exit code; Bash notices this and terminates the script immediately. We’re all safe, at least for now.

Some exceptions to this rule are necessary to be able to write useful programs. We still want to be able to use conditionals, and we can:

[[ … ]] is a conditional expression: it returns either 0 (success) or 1 (failure) depending on a condition. In the above case the condition is checking whether the first argument ($1) is an ordinary file (-f). The space after [[ and before ]] are necessary, because [[ is just another command with arguments.

 

This page is a preview of The newline Guide to Bash Scripting

No discussions yet