- Posted on
- • Questions and Answers
Trap `ERR` only for specific commands using `trap '' ERR` and `set -E`
- Author
-
-
- User
- Linux Bash
- Posts by this author
- Posts by this author
-
Mastering Error Handling in Bash: Using the trap ERR
and set -E
Commands
In this blog post, we'll explore a crucial aspect of Bash scripting: error handling. Specifically, we'll concentrate on how you can trap errors for specific commands using Bash’s trap '...' ERR
in combination with set -E
. Let’s delve into some common questions and answers to unravel this powerful Bash tool, followed by simple examples and an executable script to solidify our understanding.
Q1: What is the trap
command in Bash scripting?
A: The trap
command in Bash allows you to specify a script or command that will execute when your script receives specified signals or conditions. When used with ERR
, the trap
command is executed when a script runs into errors, i.e., whenever a command exits with a non-zero status.
Q2: What does the set -E
command do in this context?
A: The set -E
(or set -o errtrace
) command ensures that any ERR traps are inherited by shell functions, command substitutions, and commands executed in subshells. This property is essential when working with more complex scripts where commands are nested within functions or subshells.
Q3: How do I trap ERR
only for specific commands?
A: To trap errors only for certain parts of your script, you can adjust the scope of your trap '...' ERR
with set +E
to deactivate inheriting ERR traps and set -E
to reactivate it around the specific commands. This allows for more precise error handling.
Background and Examples
The general use of trap
and set -E
might still seem abstract, so let’s explore some simple examples to clarify their functionality.
Example 1: Basic Trap
Here's how to use trap
with ERR
:
#!/bin/bash
trap 'echo "Error occurred at line $LINENO."' ERR
false # This is a command that will fail
echo "This will not run after error"
The script uses trap
to print an error message whenever a command fails (returns non-zero exit status).
Example 2: Using trap
with set -E
Let’s script a scenario where we only wish to handle errors within a specific function:
#!/bin/bash
set -E # Ensure ERR trap is inherited
function error_prone_area {
trap 'echo "Error in function at line $LINENO."' ERR
return 1 # Simulate an error
trap - ERR # Remove the trap
}
echo "Starting script..."
error_prone_area
echo "Script ended cleanly."
Executable Script Example
Let’s combine these concepts into a full script that demonstrates how to selectively handle errors in specific sections:
#!/bin/bash
set -E # Enable error trace
trap 'echo "General error occurred."' ERR
function risky_function {
trap 'echo "Error specifically in risky_function at line $LINENO.";' ERR
false # This command will fail
trap - ERR
}
echo "Before entering risky function."
risky_function
echo "After risky function."
false # This command will also fail, but with the general error trap
echo "End of script."
Conclusion
Being able to handle errors on a granular level can significantly enhance the robustness and reliability of Bash scripts. With the ERR trap and the set -E
options, scriptwriters have fine-grain control over where and how errors are caught and managed. This particular trapping method is invaluable in creating scripts that react properly under various error conditions, improving debugging, and ensuring smoother runtime operations. Whether you're writing scripts for automation, deployment, or general system tasks, mastering these tools can substantially uplift your scripting prowess.
Further Reading
For more insight into error handling and advanced bash scripting techniques, consider exploring the following resources:
Advanced Bash-Scripting Guide: Provides a detailed exploration of using the
trap
command for more sophisticated script scenarios. Access it here.Bash Hackers Wiki on Traps: Offers a comprehensive breakdown of trapping signals and errors in Bash scripts for reliable error management. Learn more here.
IBM DeveloperWorks - Bash by Example: A series that includes practical examples of advanced Bash features including error handling. Explore it here.
Linux Journal - Handling Errors in Bash: Discusses different strategies for error detection and handling in Bash scripts. Read more here.
Stack Overflow Discussion on
trap ERR
and Script Robustness: Discussions and examples contributed by community experts on effective use of error trapping in scripts. Check this out here.