Skip to content

Using bash options to change the behavior of scripts

Bash provides a large number of options that can be used to control the behavior of bash scripts. This post examines some of the more useful ones and explains how to display which options are in use and which are not.

Exiting when an error occurs

If you want a bash script to exit soon as it encounters an error—any error at all—in your scripts, you can add the set -o errexit option. If your script contains a syntax error, tries to read a file that doesn’t exist, attempts to append to a file when you don’t have permission to do so, or misuses a command in some way, the script will end abruptly. Here is a simple example:


set -o errexit

tail NoSuchFile
echo -n “Enter text to be appended> “
read txt
echo $txt >> NoSuchFile

Try to run this script, and you’ll see this:

To read this article in full, please click here

Source:: Network World – Data Center