FUNDAMENTALS A Complete Guide for Beginners
Suppressing output in Bash implies preventing commands’ outputs from being displayed in the terminal. Whether it’s to keep the terminal clean, improve script performance, or simply discard unwanted information, there are quite a handful of scenarios where a user may want to suppress the output of a command. For all these cases, Bash provides various techniques to manage and manipulate command output.
In this article, I will discuss how you can suppress output in Bash with some practical cases.
Different Cases of Suppressing Output in Bash
While working with Bash scripts, often users may need to suppress only output, displaying the error messages for debugging purposes. Or, maybe users want only the outputs to display not the error messages for a cleaner terminal, or both the output & error messages. Anyway, Bash provides solutions for each of the criteria. Check out the following cases to suppress output in Bash:
Case 1: Suppressing Output Messages
The simplest and most common approach to suppress output in Bash is by redirecting it to the null device, /dev/null. This special file in Unix-like systems discards all data written to it.
To suppress output message, use the >
redirection operator following the syntax below:
Command >/dev/null
Case 2: Suppressing Error Messages
Use the redirection operator 2>
to suppress only the error messages generated by a command. In this case, the number 2 indicates the standard error (stderr) file descriptor. See the command syntax below:
Command 2> /dev/null
Case 3: Suppressing Both Output and Error Messages
To suppress both the output and error messages of a command, you can combine the previous two operators using the &>
redirection operator. This will redirect both standard output (stdout) and standard error (stderr) to the null device.
Here’s the syntax to suppress both output and error messages:
Command &> /dev/null
3 Practical Examples of Suppressing Output in Bash
Following I will show three practical examples for each of the discussed suppressing cases.
Example 1: Suppress Both Output & Error Messages While Silently Checking if a File Exists
Utilizing the redirection operator &> can help suppress both the error messages & output while silently checking whether a specified file exists in the system or not. Go through the below script to check how that works:
#! /bin/bash
file="/home/munny/input.txt"
if ls "$file" &> /dev/null; then #Checks files list & redirects output & error messages to /dev/null
echo "File exists"
else
echo "File does not exist."
fi
Here, the ‘file’ variable is assigned the path to the file a user wants to check. The ls command lists the file but the output is redirected to /dev/null
using &>
effectively silencing it. The if statement checks the status of the ls command and simply prints if the file exists or not.
The output is only echoing the message “File exists”. It’s not displaying the output or any of the error messages (if exists) of the ls command. Cause they have been redirected to the null device.
Example 2: Suppress Only Error Messages in a Loop
To suppress only the error messages in a loop, use the 2> operator. Using this redirection operator doesn’t suppress the script output messages.
Here’s a script to suppress only the error messages while using a for loop:
#! /bin/bash
directories=("dir1" "dir2" "dir3" "nonexistent_dir")
for dir in "${directories[@]}"; do
if [-d "$dir" 2>/dev/null ]; then #Checks the existence of dir1, dir2, dir3, nonexistent_dir
echo "Directory '$dir' exists."
else
echo "Directory '$dir' does not exist."
fi
done
The script defines an array called ‘directories’ with a list of directory names. Then, it loops over each directory using a for loop and assigns the current directory to the ‘dir’ variable. Inside the loop, -d "$dir"
checks if the directory exists. And the error messages related to nonexistent directories are redirected to /dev/null using 2>/dev/null
. In the last lines, the echo commands print the quoted messages if exist then ‘exists’ & if not then ‘does not exist’.
After executing the script, it’s only showing the output message but not any error messages. Even though as you can see there is a non-existent directory name in the list.
Example 3: Suppress Output While Executing a Command in Silence
The curl command can perform a silent execution while suppressing its output to the null device. For example, here’s a script that will only print either of the quoted messages inside the echo command based on if the condition meets or fails.
#! /bin/bash
url="https://linuxsimply.com"
if curl -s "$url" > /dev/null; then #Checks the URL request validity
echo "Request succeeded"
else
echo "Request failed"
fi
Here, the script uses the curl command to make an HTTP request to the specified URL “https://linuxsimply.com”. The command option –silent suppresses the progress meter & then the command output messages are redirected to /dev/null using > /dev/null instead of displaying on the terminal.
The if statement checks the exit status of ‘curl’. If the request succeeds (i.e. the exit status is 0), it echoes “Request succeeded”. Otherwise, echoes “Request failed”.
Executing the script only gives the “Request succeeded” message. It suppresses all the output messages of the curl command.
Conclusion
To sum up, some output you will get from running a command or executing a Bash script will be too long & tedious for your display. In some cases, you will only need the error messages to print. So learning how to suppress output or error messages is an important task to be skilled at. Hope this article helps you leaning the process!
People Also Ask
Related Articles
- How to Print Output in Bash [With 6 Practical Examples]
- What is Echo Command in Bash [With 3 Practical Examples]
- How to Echo New Line in Bash [6 Practical Cases]
- Cat Command in Bash [With 4 Practical Examples]
- How to Save Bash Output to File? [3 Practical Cases]
- How to Save Bash Output to Variable? [With Practical Cases]
- How to Set Command Output to Variable in Bash [2 Methods]
- How to Change Color of Output Using Bash Script? [Easy Guide]
<< Go Back to Bash Output | Bash I/O | Bash Scripting Tutorial