How to Set and Pipefail in Bash Scripts on Linux

Spotting bugs in a multi-piped process can be a real pain unless you know how to utilize set and pipefail in Bash Scripts on Linux-based systems. These are meant to promisingly dictate failures around Bash scripts and eventually, tackle them with ease.

If you’ve been working around Bash Scripts, you already know that they work great. Not only because bash scripts are quick to write, but because they don’t require any form of compilation makes the overall process very efficient. However, the bash scripts also aren’t far from issues and errors.

The issues come in as soon as you start employing an external utility. The third-party program will immediately terminate the operations. After that, it forwards an error code to the shell whenever a bash script fails to execute. Not just that, you may also see an error message getting printed on the concerned Terminal.

There is no denying that the issues sheltering within program execution can lead to many annoying situations, especially when dealing with something critical. In case you’re wondering about the workaround, the answer is simple, check the return error code and devise a solution for it.

The Importance of Set and Pipefail in Bash Scripts on Linux

Keeping the returned error code under check is no joke, and when you’re concerned about a process that’s piped into another, things can get really challenging. The calculation is simple; you can’t successfully predict the exact source of the return code as the same can emerge from anywhere in the process.

When the set and pipefail commands in Bash scripts come into play, it significantly aids in the seamless detection of any underlying errors. Thus, eventually making you capable of deciding how to act.

How to Use Set and Pipefail in Bash Scripts on Linux

Having offered a brief idea of Bash Script errors and how the set and pipefail command can help deal with the trouble, it is the perfect time to learn how to use them. We’ve sectioned the entire process in the following steps to ensure you’ll have a better understanding of the situation at hand.

Step 1: Understanding the Problem

The first step, of course, is understanding what the issue is about. Suppose we’ve got an unimportant Bash script that echoes two specific text lines to the concerned Terminal. Where one says, “This is the first one to happen,” the other states, “This is the second one to happen.”

If you don’t know, it is possible to execute the scripts quickly after copying the text into any existing editor and saving the same by any desired name, “scriptnew.sh“, for instance, you will have to input:

#!/bin/bash
echo This is the first one to happen
echo This is the second one to happen

Output:

Set and Pipefail in Bash Scripts

Making the Script Executable

To make the script executable, pass the chmod command. The required format should look something like this:

$ chmod +x script new.sh
Making the Script Executable

Remember that it is essential to invoke the command on each script when and if there is a desire to execute the commands on your system. Once done, you’ll notice that the two lines sitting on the script get displayed on the Terminal.

The Error Rising Situation

For making the scenes with errors more distinct, let’s head over and create one. What we’re going to do is alter the script a bit. And by alteration, we mean modification where we’d ask the ls command to list something that isn’t actually available. Once done, suppose I save the same script and name it “modifiedscript.sh”.

What do you think happens as soon as we try to execute the script? Of course, you’ll see an error message stating that no such file exists in the directory. Now, although the ls command failed, you’ll see that the script continues to run, and despite the error, the return code remains “0,” which means you’ve successfully done what you came to do here.

pipefail

Step 2: Decoding the Scenes Around Error

As the return code is “0”, even when the process has an error, it reflects that there are two issues corresponding to the scenario. While the first is that the script continued running despite carrying an error, the second issue is home to somewhere around another process that checks the success status of any operation.

Step 3: The Set -e Option

With the set -e or the set exit option, you’re looking at the utility that allows the initiation of an additional script to easily change the behavior. Let’s say the script says, “newscript3.sh”.

Input:

#!/bin/bash 
set -e option
echo This is the first one to happen echo 
ls no-filename
This is the second one to happen

Now on running the script, you’ll notice a non-zero return code. That’s the effect of the set -e option. You can confirm it by invoking the echo command.

Step 4: Others Ways to Deal with Failures

The set -u command is another way to deal with bash script failures. Like the -e option, it is also used for flawed variables, but when it gets an error, the corresponding script exits immediately.

Using the set -f command will help when your script bags some incompatibilities. It seamlessly disables the filename expansion and aids in addressing all sorts of issues.

Sealing the Scripts with X

Without any doubt, using the set -e option is fantastic. But at the same time, you should understand that utilizing the set -x or the set execute, and print option is meant to behave like an absolute lifesaver while writing scripts. 

What the Set -X option does is that it prints all the commands and its associated parameters. In simple words, it provides a rough but efficient execution trace. Thus spotting and identifying flaws won’t be much of a big deal anymore.

And that wraps up today’s session on how to use set and pipefail in Bash Scripts on Linux. Here you’ve learned the importance of set and pipefail command-line utility, especially when you’re stuck midways with an error setting home inside a multi-piped process.

If this guide helped you, please share it.

Leave a Reply
Related Posts