What Does set -e Mean in a Bash Script?

Linux provides the pre-installed “set” command to manipulate the shell arguments and prints the bash script variables. It comes with a huge variety of supported options/flags in which the “-e” is useful to instantly stop the execution of a bash script at the point of non-zero exit status.

By default, the bash interprets the whole script and does not stop execution at any error. The “set -e” exits the script quickly if any command execution fails and can’t execute the other commands. It is useful to identify the error occurrences in the script for debugging them.

Considering its importance, this post explains:

  • How Does set -e Command Work in a Bash Script?
  • Use “set -e” at the Start
  • Use “set -e” Outside the Function
  • Use “set -e” Inside the Function

How Does set -e Command Work in a Bash Script?

The “set -e” command is used in the bash script to quickly exit the script if any of the commands give an exit status 1. The “exit 1” means the command is not executing due to some syntax or logical error.

Its working depends on its implementation in the bash script. Let’s check this with the help of various examples.

Example 1: Use “set -e” at the Start of the Bash script

There is an existing bash script “err.sh” opened in the nano text editor:

$ nano err.sh

Script

#!/bin/bash
set -e
a=10
echo $a"
echo "This is the output"

The description of the “err.sh” is illustrated here:

  • The “#!/bin/bash is the “Bash Shebang,” which will execute the “err.sh” script in the “bash” shell. 
  • The “sete” command will stop the execution of the bash script, where the execution of any command will fail.
  • The variable “a” is declared that contains an integer value, i.e., “10”.
  • The “echo” command has a syntax error in opening and closing the delimiter.“
  • The last “echo” command will print its statement.

Press “Ctrl+S” to save and “Ctrl+X” to exit the editor.

Run the script in the terminal:

$ ./err.sh

The output shows an error at line 5, and the other script commands did not execute.

Example 2: Use “set -e” Outside the Function in Bash Script 

The “set -e” command can also be implemented in the bash script functions to throw an error message and stop the script execution. Let’s understand this with a bash script.

Script:

#!/bin/bash
set -e
function hello()
{
echo Hello Linux!"
}

echo "There is an error in hello function"

The above code block script contains:

  • set -e: Exits the bash script at the line where an error comes.
  • hello:  Denote s function with no argument having an “echo” command.
  • echo: Last command in the script to print the enclosed statements.

Save the file and exit the editor.

Lastly, execute the “fun.sh” script for checking its output:

$ ./fun.sh

The output shows the error in line 8, due to which the further commands of the “fun.sh” script are not executed.

Example 3: Use “set -e” Inside the Function in Bash Script 

The function also assists the user in implementing the “set -e” command inside the function. Its working is not effect by its position, whether inside or outside the function. 

In this scenario, it is placed inside the function of the bash script.

Script:

#!/bin/bash
function hello()
{
set -e
echo Hello Linux!"
}
echo "There is an error in hello function"

At this time, the “set -e” command is written inside the “hello” function:

Execute the script in this way:

$ ./fun.sh

The output is the same as outside the function 

Conclusion

In a Bash script, the “set -e” identifies the error location and exits the script execution quickly without the execution of other commands. The “set -e” is generally implemented at the start of the “bash” script next to the “Bash Shebang” line however it can be placed “inside” or “outside” the function. 

This post has provided a deep insight into the objective, working, and usage of “set -e” in a bash script.