In Bash, there are quite a handful of scenarios where you may want to suppress the output of a command. Whether it’s to keep your terminal clean, improve script performance, or simply discard unwanted information. 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.
Key Takeaways
- Learning different output-suppressing methods.
- Overviewing some practical examples where you may need to suppress outputs.
Free Downloads
Different Cases of Suppressing Output in Bash
While working in Bash scripts, you may need to suppress your output but you want to display the error messages for debugging purposes. Or, maybe you want the outputs to display only 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:
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 using this method, just use the ‘>’ redirection operator.
Use the command syntax >
Command >/dev/null
Case 2: Suppressing Error Messages
Sometimes, you might want to suppress only the error messages produced by a command. For that, use the ‘2>’ redirection operator. Here, the number ‘2’ represents the standard error (stderr) file descriptor.
Use the command syntax >
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.
Use the command syntax >
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
In this first example, I will show you by executing a Bash script how you can suppress both the error messages & output while silently checking whether a specified file exists in the system or not. Please go through the below steps to check how that works:
➊ At first, launch your Ubuntu Terminal application.
➋ Now, write the following command to open a file in the nano text editor:
nano File.sh
➌ Afterward, write the following script in the nano editor:
#! /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
➍ Then, press CTRL+S to save the file & press CTRL+X to exit the nano editor.
➎ After that, use the following command to make the script executable:
chmod u+x File.sh
- chmod: Changes the permission of files and directories.
- u+x: Argument with chmod command to add the executable permission for the user.
- sh: File which you want to make executable.
➏ Finally, run the script by the following command:
./File.sh
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
In this bash script example, while using a for loop, I will suppress only the error messages but will display the script output messages.
Script (string.sh) >
#! /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
Now, run the script by the following command:
./string.sh
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
Here, in this script, I will show the curl command execution in silence while suppressing its output to the null device. It will only print either of the quoted messages inside the echo command based on if the condition meets or fails.
Script (string.sh) >
#! /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
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”.
Now, run the script by the following command:
./string.sh
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