Bash

How to undo the effect of set -e which makes bash exit immediately if any command fails

19 September 2026 · 7 min read

How to undo the effect of set -e which makes bash exit immediately if any command fails

Working with Bash scripts often involves handling potential errors gracefully. The set -e command is a powerful tool that instructs Bash to exit immediately if any command fails. This can be incredibly useful for preventing cascading failures and ensuring that your script doesn’t proceed when something unexpected happens. However, there are scenarios where you might need to undo the effect of “set -e” temporarily or permanently. Perhaps you want to allow a specific command to fail without halting the entire script, or you’ve inherited a script using set -e and need to modify its behavior. Understanding how to manage and control this setting is crucial for writing robust and flexible Bash scripts. This article will delve into various methods to disable or override set -e, providing practical examples and best practices.

Understanding the “set -e” Command

The set -e command, also known as set -o errexit, is a Bash built-in that alters the shell’s behavior when a command exits with a non-zero status (indicating failure). By default, Bash continues executing commands even if one fails. With set -e enabled, the script terminates immediately upon such a failure. This is particularly useful for automation and deployment scripts where continuing after an error could lead to data corruption or system instability. Think of it as a safety net that prevents your script from stumbling blindly into disaster.

However, the “fail-fast” approach isn’t always desirable. There are legitimate cases where you expect a command to potentially fail and want to handle that failure within the script itself. For instance, you might be probing for a network service that isn’t always available, or you might be attempting to create a directory that already exists. In these situations, you need a way to temporarily disable or override the set -e behavior. According to the Bash man page, “The shell exits immediately if a command exits with a non-zero status.” [1]

Consider a scenario where you’re trying to connect to multiple servers, and only one needs to be accessible for the script to succeed. Using set -e would cause the script to exit on the first failed connection attempt, preventing it from trying the other servers. Therefore, mastering techniques to selectively bypass set -e is essential for crafting versatile and resilient Bash scripts. This ensures your scripts can gracefully handle expected failures without abruptly terminating.

Methods to Disable “set -e” Temporarily

There are several effective ways to temporarily disable the effects of set -e, allowing specific commands to fail without causing the script to exit. One common method is to use the || true construct. By appending || true to a command, you effectively ensure that the entire expression always returns a zero exit status, regardless of the command’s actual outcome. This tricks set -e into thinking the command was successful, preventing the script from terminating.

Another approach involves wrapping the potentially failing command in a subshell and checking its exit status explicitly. This gives you more control over how the failure is handled. You can use an if statement to examine the $? variable, which holds the exit status of the last executed command, and take appropriate action based on the result. This allows you to log the error, retry the command, or execute alternative code paths.

Here’s an example of using || true:

command_that_might_fail || true echo "This will always execute, even if the command above fails." 

And here’s an example of using a subshell and checking the exit status:

(command_that_might_fail) if [ $? -ne 0 ]; then echo "Command failed, but we're continuing." fi 

Re-enabling “set -e” After Disabling

If you’ve temporarily disabled set -e using one of the methods described above, it’s crucial to re-enable it to maintain the script’s error-handling integrity. Failing to do so can lead to unexpected behavior and make it harder to debug issues later on. The simplest way to re-enable set -e is to simply include the command set -e again in your script after the section where you disabled it.

It’s good practice to clearly delineate the sections where set -e is disabled and re-enabled, perhaps with comments explaining why you’re doing so. This makes the script more readable and maintainable. For example:

set +e Disable set -e temporarily command_that_might_fail || true set -e Re-enable set -e 

Alternatively, you can use the set -o errexit and set +o errexit commands to achieve the same effect. set -o errexit enables set -e, while set +o errexit disables it. Using descriptive comments alongside these commands significantly enhances the clarity and understandability of your scripts for yourself and other developers.

Featured Snippet Paragraph: To temporarily disable set -e in Bash, append || true to the command that might fail. This ensures the command always returns a zero exit status, preventing the script from exiting. For example: command_that_might_fail || true. Remember to re-enable set -e afterwards with set -e to maintain error handling.

Best Practices and Considerations

While set -e is a valuable tool, it’s important to use it judiciously and understand its limitations. Over-reliance on set -e can sometimes mask underlying problems or make it difficult to debug complex scripts. It’s often better to handle errors explicitly whenever possible, providing informative error messages and taking corrective actions.

Consider the following best practices:

  • Use set -e as a default: Start your scripts with set -e to ensure that errors are caught early.
  • Handle expected failures explicitly: Use if statements and error checking to gracefully handle situations where a command might fail.
  • Provide informative error messages: When an error occurs, log detailed information about what went wrong to aid in debugging.
  • Re-enable set -e promptly: After temporarily disabling set -e, remember to re-enable it as soon as possible.

Furthermore, be aware that set -e doesn’t always behave as expected in all situations. For example, it doesn’t apply to commands within while or until loops unless those commands are part of a pipeline. Understanding these nuances is crucial for writing robust and predictable scripts. According to Stack Overflow, “set -e only applies to commands that are not part of a pipeline or a while/until loop condition.” [2]

Here’s a summary of scenarios where set -e might not behave as expected:

  • Commands within while or until loop conditions.
  • Commands that are part of a pipeline unless the last command fails.
  • Commands listed after an && or || operator (only the last command in the chain triggers the exit).
Infographic here: illustrating common scenarios where set -e might be bypassed and how to handle them.
FAQ ---
What does "set -e" do in a Bash script?
It causes the script to exit immediately if any command exits with a non-zero status (indicating failure).
How can I temporarily disable "set -e"?
You can append `|| true` to the command that might fail, or wrap the command in a subshell and check its exit status explicitly.
How do I re-enable "set -e" after disabling it?
Use the command `set -e` again in your script.
Does "set -e" always work as expected?
No, it doesn't apply to commands within `while` or `until` loops unless those commands are part of a pipeline, or commands after && or || unless the last one fails.
What is the alternative to "set -e"?
Explicitly check the exit status ($?) of commands using if statements and handle errors accordingly, providing more control and informative error messages.
1. Identify the section of your script where you need to bypass the "set -e" behavior. 2. Choose a method for disabling "set -e" temporarily (e.g., || true or subshell with exit status check). 3. Apply the chosen method to the specific commands that might fail. 4. Immediately after the section where you disabled "set -e", re-enable it using set -e. 5. Test your script thoroughly to ensure that errors are handled correctly and that "set -e" is functioning as expected.

By understanding how to undo the effect of “set -e” and when to do so, you can write more robust, flexible, and maintainable Bash scripts. Remember to use these techniques judiciously, always prioritizing clear error handling and informative error messages. Always remember that explicit error handling often provides superior control and clarity over relying solely on set -e. Understanding how to manipulate Bash’s behavior using these techniques ensures your scripts are more resilient and predictable in various environments. This approach not only enhances the reliability of your scripts but also simplifies the debugging process. For further reading on Bash scripting and error handling, refer to the Advanced Bash-Scripting Guide. [3]. We encourage you to explore other options for improving your scripts with this helpful guide.

Question & Answer :
After entering set -e in an interactive bash shell, bash will exit immediately if any command exits with non-zero. How can I undo this effect?

With set +e. Yeah, it’s backward that you enable shell options with set - and disable them with set +. Historical raisins, donchanow.