Just Learn Code

Mastering Bash: Error and Signal Handling Techniques

In the world of scripting and automation, Bash is one of the most commonly used shells. Bash, like any other scripting or programming language, is not perfect and may encounter errors or failures during execution.

In order to handle errors and failures properly, Bash provides various techniques such as exit codes, error handling, and signal handling. In this article, we will explore these concepts in depth and provide useful tips for using these techniques in your Bash scripts.

1) Error Handling in Bash

Exit Codes in Bash

Exit codes are a crucial component of error handling in Bash. Every command executed by Bash returns an exit code, which is an integer value that represents the success or failure of the command.

An exit code of 0 represents success, while any non-zero value indicates an error or failure. This makes it easy to detect command failures and take necessary action.

For example, if you execute the curl command to download a

file, and the command fails due to a network issue, it will return a non-zero exit code. By checking the exit code, you can detect that the command failed and take appropriate action, such as notifying the user or retrying the command.

Bash Error Handling with Simple Exit Codes

Exit codes can be easily checked using conditional statements in Bash. You can use the syntax “if [ $?

-eq 0 ]; then” to check the exit code of the previous command and execute a block of code if the exit code is 0. For example, consider the following snippet of code that executes the ls command and checks its exit code:

ls /path/to/directory

if [ $?

-eq 0 ]; then

echo “Directory listing successful”


echo “Directory listing failed”


In this code, if the ls command returns an exit code of 0, the “Directory listing successful” message is printed. Otherwise, the “Directory listing failed” message is printed.

Exit on Error

An alternative approach to error handling with exit codes is to exit the script immediately on error detection. This technique is useful when you want to ensure that the script does not continue executing when an error occurs.

To achieve this, you can use the “set -e” command in your script to enable exit on error. This causes the script to immediately exit with a non-zero exit code when any command returns a non-zero exit code.

For example:

set -e



In the above code, if the command_that_might_fail fails, the script will immediately exit with a non-zero exit code and not execute the command_that_should_succeed.

2) trap Command in Bashto trap Command

The trap command is a powerful tool for signal handling in Bash. Signals are software interrupts sent to a process to notify it of some event, such as a process being terminated or a user interrupt.

The trap command allows Bash programmers to de

fine actions to be taken when signals are received by a process. This can be useful for graceful cleanup of resources when a process is terminated, or for handling unexpected user input.

Listing Signals with

trap -l Command

The “

trap -l” command can be used to list all the signals that can be trapped by a Bash script. Signals are identi

fied by names such as SIGINT (interrupt signal) and SIGTERM (termination signal).

For example, to list all available signals, you can use the following command:

trap -l

This will output a list of all available signals along with their corresponding signal numbers.

Determine Commands Linked with a Certain Signal Using trap -p SIGSPEC

The “trap -p SIGSPEC” command can be used to determine the commands that are linked with a certain signal. This is useful when troubleshooting unexpected behavior of scripts.

For example, to determine the commands linked with the ERR signal, you can use the following command:

trap -p ERR

This will output the commands that are executed when the ERR signal is received by the script. Example: Printing Signal Commands for Speci

fied Signals

To demonstrate the use of the trap command, consider the following example Bash script:


# Cleanup function to be executed on script exit or interruption


echo “Cleanup function executed”


# Trap signals and link them to the cleanup function


echo “Press Ctrl+C to test SIGINT signal handling”

sleep 10

In this script, a function named cleanup is de

fined that will be executed when the script exits or is interrupted. The “trap” command is used to link this function to several signals, including EXIT (script exit), SIGINT (Ctrl+C interrupt), SIGTERM (termination signal), and ERR (command error).

When the script is executed, it will

first print a message and then sleep for 10 seconds. During this time, the user can test the SIGINT signal handling by pressing Ctrl+C, which will trigger the cleanup function.


In this article, we have explored the basics of error handling and signal handling in Bash. By using these techniques, you can ensure that your Bash scripts are robust and reliable even in the face of unexpected events.

The use of exit codes, conditional statements, and the trap command are powerful tools for any Bash programmer. In addition, the ability to list available signals and determine linked commands can be useful for troubleshooting unexpected behavior.

By being familiar with these concepts and techniques, you can become a more effective Bash programmer. In conclusion, error handling and signal handling are essential concepts in Bash programming.

Bash provides several techniques to handle errors and signals, such as exit codes, conditional statements, and the trap command. By being familiar with these techniques, you can improve the robustness and reliability of your Bash scripts.

Remember to check the exit codes of commands, use conditional statements for error handling, and use the trap command for signal handling. With these tips, you can become a more effective Bash programmer and ensure that your scripts are handling unexpected events in the best possible way.

Popular Posts