Error handling in Bash
This article introduces error handling in bash. Remember, understanding exit codes, options such as errexit and trap allow us to build robust scripts and manage bash problems more effectively.
Exit Codes in Bash
Handling errors based on exit codes is a standard technique for detecting command failures. This is especially true for external commands.
The curl command in bash is a good example of handling problems based on known error codes. In contrast to user-defined routines, external command error codes are fully documented.
${?}
Stores the exit status of the last command executed before the given line. Code 0 means the command was executed successfully. Otherwise, something went wrong.
case {?} in
0) {
true # ok
} ;;
*) {
false # something went wrong
}
Bash error handling can be performed using simple exit codes. You can try until you find a lazier solution. At least, anyone would do so after performing some conditional handling of the problem based on the error code.
Error exit
Exiting on error is undoubtedly the most useful error detection and handling capability that bash programmers lack in the first place.
Trap, Exit, and Error
trap allows us to specify a command to be executed when the shell receives a signal. SIGNAL SPEC is the name of the signal. Some common signals are EXIT
, ERROR
, , DEBUG
and RETURN
.
We can use the trap -l command to list more signals. We can also use trap -p SIGSPEC to determine which commands are associated with a certain signal.
For example, we might want to determine which commands correspond to the ERR signal. In that case, we could type the following command line:
trap -p ERR
If the output is empty, trap has not yet associated any command with the signal.
_() { echo oops ; }
trap _ ERR EXIT
Print signal commands now do not produce empty results.
enter:
trap -p EXIT ERR
Output:
trap -- '_' EXIT
trap -- '_' ERR
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Getting the absolute path in Bash
Publish Date:2025/03/21 Views:60 Category:OPERATING SYSTEM
-
In this Bash article, we will learn different ways to get the absolute path in Linux. We will also learn some different Linux commands to get the absolute path of a file. Before we begin, we need to understand the basic concepts of absolute
Difference between Bash Nohup and &
Publish Date:2025/03/21 Views:151 Category:OPERATING SYSTEM
-
This short article introduces the nohup command and the control operator to run Linux processes in the background through Bash. In addition, we will further study the key differences between nohup and . Running Linux Processes in the Backgr
Renaming Files in Bash
Publish Date:2025/03/21 Views:89 Category:OPERATING SYSTEM
-
With the help of Bash scripts, you can automate your tasks. File renaming is a common task on various systems. You can rename all the files manually. However, if your file names have a sequence, it is better to automate this task. This way
Open Emacs in Bash
Publish Date:2025/03/21 Views:141 Category:OPERATING SYSTEM
-
This article will show you how to open Emacs from within Bash. We will also discuss how to install the Emacs text editor. Install EMACS in your system Suppose you don't have Emacs in your system. You can easily install it in your system wit
Clear the Terminal Screen in Bash
Publish Date:2025/03/21 Views:145 Category:OPERATING SYSTEM
-
There are various ways to clear the terminal in bash script. This article will discuss 3 methods to clear the terminal. Use tput reset to clear the terminal screen. The first method uses the keyword tput reset to clear the screen. When your
Reload .bash_profile from the command line
Publish Date:2025/03/21 Views:67 Category:OPERATING SYSTEM
-
In the shell, .bash_profile is used to customize the configuration of user settings. It is stored in the root directory or home directory and is mostly hidden from other users. This file holds all the configuration of the shell and is also
Check if input parameter exists in Bash
Publish Date:2025/03/21 Views:191 Category:OPERATING SYSTEM
-
When we create a Bash script, we may want to use parameters in our script to run successfully. Therefore, we need to create a script to check the number of input parameters used by the user in the script. All of this prevents unexpected beh
Getting optional input arguments in Bash
Publish Date:2025/03/21 Views:180 Category:OPERATING SYSTEM
-
Sometimes we need to create a dynamic function which can be executed in both without passing any arguments or without passing any arguments. For this we need to set some default values for those arguments so that if the arguments are
Using Double and Single Pipes in Bash
Publish Date:2025/03/21 Views:136 Category:OPERATING SYSTEM
-
In Bash, the double pipe || is also known as the OR operator in other programming languages. On the other hand, the single pipe | is known as a pipeline. In this article, we will see how to use double pipe (also known as OR) and pipelines i