How to Check if a Command Succeeded in Bash

29/12/2020
Whether you’re writing a script or just being curious, it’s useful to know that the command succeeded without any issue. Personally, bash scripting is the place where this feature is most needed. When you’re scripting a series of commands and the previous output impacts the later, it’s better to verify if it worked.

In this article, I’ll be showcasing a number of ways you can verify if your bash command was successful. There’ll be script examples that’ll showcase its usage. However, depending on what command you run, the verification may require different tactics.

Checking command Succeeded

Whenever a command runs, the return value of the command is stored in a specific bash variable. For the first example, let’s run the package manager to update the system. In my case, it’s Ubuntu, so the command would be something like this.

$ sudo apt update && sudo apt upgrade -y

Here, from the output, we can easily say that the command ran successfully. Now, every command run in bash shell returns a value that’s stored in the bash variable “$?”. To get the value, run this command.

$ echo $?

If a command succeeded successfully, the return value will be 0. If the return value is otherwise, then it didn’t run as it’s supposed to. Let’s test it out. Run the same update command but this time, interrupt the command by pressing “Ctrl + C”.

Now, check the value of the bash variable.

$ echo $?

The value is not 0, so there’s definitely an error. Here, we forced the command to break. How could this be useful in bash scripts? Here’s a quick example of how to use it on the bash script. Save the script as a text file with .sh as the file extension.

#!/bin/bash
<command>
if [ $? -eq 0 ]; then
   echo OK
else
   echo FAIL
fi

Make the file executable.

$ chmod +x demo.sh

Now, run the script.

$ ./demo.sh

After running any command, bash will update the value of the variable. In this case, after running the echo command, we can determine if it ran successfully or not. Swap the echo command with anything you like and voila!

Here’s another interesting method that can confirm if the command succeeded. It’s just a one-line command that’s very simple.

$ <command> && echo SUCCESS || echo FAIL

Here, the command is split into two sections by the “||” sign. If the first command runs successfully, the first echo command must run. Otherwise, the second echo command will run. Let’s check it out with an example.

$ sudo apt update && echo SUCCESS || echo FAIL

The first part didn’t succeed, so the first echo command was omitted. Instead, the second echo command ran, indicating that the first part didn’t run successfully. What if it ran successfully?

The first echo command was activated.

Here’s another example of a bash script.

#!/bin/bash
if <command>; then
     echo “Success”
else
     echo “Failure, exit status: $?
fi

Run the script.

$ ./demo.sh

If the command didn’t succeed, the output would be different.

Which bash script to use? I strongly recommend the first one where the command is run first, then the value of “$?” is extracted in a variable and then, perform whatever you want depending on the value of the variable.

Final thoughts

Bash is a powerful shell with a strong scripting feature. If you want to know if the previously-run command succeeded, these are some of the most reliable methods.

Which method to use? It depends on what’s the goal you want to achieve. For usage in the terminal and command line, using the single command example is the best way to go. As for the bash scripting, feel free whichever method serves you the best, especially the first script example I demonstrated.

Want to make your life easier with bash? Learn more about how to set bash aliases and some popular and handy aliases.

Enjoy!

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Bash jq command

JSON data are used for various purposes. But JSON data can’t be read easily from JSON file by using bash script like...
29/12/2020

Bash History Search

We need to type various commands in the shell for various purposes. Sometimes we need to execute same commands multiple...
29/12/2020

How to read file line by line in Bash script

How would you write a Bash script that can process a text file one line at a time.  First you need a syntax and approach...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024

Dịch vụ thuê mua proxy giá rẻ an toàn, tốc độ cao
13/05/2024

Thuê mua proxy V6 uy tín, chất lượng tại đâu?
11/05/2024

Thuê mua proxy Tiktok tăng doanh thu, hiệu quả cao
11/05/2024