In this tutorial, we will learn about what does bash -e flag means in script. In bash scripting, set supports many flags such as -u, -x, -e, -o pipefail, -E etc. Each flag has a meaning and used based on user requirements and use cases. We will specifically discuss about set -e flag in this tutorial and understand the usage of this flag in bash scripting.
What does Bash set -e Flag Means in Script ?
In Bash scripting, ‘set‘ command is used to change the behavior of the shell. The ‘-e’ flag is particularly important for error handling in Bash scripts. When ‘set -e’ is used in a script, the script will exit immediately if any command it runs exits with a non-zero status. We will cover three use case where we will use ‘set -e’ flag in bash scripting in the next section.
Use cases to Use Set -e Flag in Bash Script
Also Read: List All Running Services in Linux: [Using 4 Tools]
Use case-1: Use at the Beginning of Script
We can use set -e flag at the the beginning of a bash script. This will ensure that if any command fails, the script execution stops and exit. We will understand the working of this flag using below example. We have created a bash script which does two job.
- Switch to a given directory PATH
- Prints the elements of an existing array
We have used set -e flag at the beginning of script. The path stored in PATH variable is non-existing. so script will throw the error and exit. Hence, it will not print the array elements. Save below code in a file and execute as shown below.
#vi flagUsage.sh #!/bin/bash set -e # Declare an array with elements 2, 5, and 8 declare -a num=(2 5 8) PATH='/opt/user/linux' # Switch to $PATH cd "$PATH" # Iterate through the elements of the array and print them for i in "${num[@]}" do echo "Number from array is: $i" done
OUTPUT
# sh flagUsage.sh test.sh: line 10: cd: /opt/user/linux: No such file or directory
Let us now disable the set -e flag in the script and see what happens. It will now throw error and continue with executing the rest of the code.
# vi flagUsage.sh #!/bin/bash set -e # Declare an array with elements 2, 5, and 8 declare -a num=(2 5 8) PATH='/opt/user/linux' # Switch to $PATH cd "$PATH" # Iterate through the elements of the array and print them for i in "${num[@]}" do echo "Number from array is: $i" done
OUTPUT
# sh flagUsage.sh test.sh: line 10: cd: /opt/user/linux: No such file or directory Number from array is: 2 Number from array is: 5 Number from array is: 8
Use case-2: Use as Specific Command
We can also use set -e flag as one of the command. Think of a situation where you want a piece of code to get executed irrespective of other part of the script. In such case, we will use set -e as a command for only that part of code which may cause error. In the below example, we have modified the previous code. Here, we will first print the elements of array. After printing the elements, We have used the command set -e. When we switch to the directory $PATH, script will throw error and immediately exits without print the last echo command as shown below.
# vi flagUsage.sh #!/bin/bash # Declare an array with elements 2, 5, and 8 declare -a num=(2 5 8) PATH='/opt/usr/linux' # Iterate through the elements of the array and print them for i in "${num[@]}" do echo "Number from array is: $i" done set -e cd "$PATH" echo "Script executed successfully"
OUTPUT
# sh flagUsage.sh Number from array is: 2 Number from array is: 5 Number from array is: 8 test.sh: line 16: cd: /opt/usr/linux: No such file or directory
Use case-3: Use in Subshell
There are use cases where we can use set -e flag in a subshell. Observe below code, we are trying to switch the directory in two subshell. The first subshell will throw the error and continue with next command which is execution of next subshell. In the second subshell we have used set -e flag. So here the error will be thrown while switching to the directory $PATH and script will exit immediately without executing the echo command in second subshell.
# vi flagUsage.sh #!/bin/bash PATH='/opt/usr/linux' echo "Executing command without set -e flag" ( cd "$PATH" ) echo "Executing command with set -e flag" ( set -e cd "$PATH" echo "Script executed successfully" )
OUTPUT
# sh flagUsage.sh Executing command without set -e flag test.sh: line 8: cd: /opt/usr/linux: No such file or directory Executing command with set -e flag test.sh: line 14: cd: /opt/usr/linux: No such file or directory
Summary
We have only covered to use the flag set -e in bash script. In real world code, we handle the commands errors using various tools so that the execution is not stopped. For example, we can use ‘trap’ command to handle the command error.