How to Suppress Output in Bash [3 Cases With Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

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

EXPLANATION

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.

Run a Bash Script & suppress output & error messages.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

EXPLANATION

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’.

Suppress error messages while running a Bash scriptAfter 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

EXPLANATION

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”.

Suppress output of a command while running a Bash scriptExecuting 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

How do you suppress any output in bash?
In Bash, you can suppress the output of a command by redirecting it to the null device (‘/dev/null’). The null device is a special device file that suppresses everything written to it. Use the symbol ‘>’ for redirecting output, ‘2>’ for redirecting error & ‘&>’ for both output & error to the null device.

How do I hide command output?
To hide a command output, redirect the output to the special device file ‘/dev/null’. As this file removes everything written to it. For that use the command syntax, “Command > /dev/null”.

How do I suppress messages in bash?
To suppress messages in Bash, you can both redirect the standard output (stdout) and standard error (stderr) to the null device (‘/dev/null’), as it discards everything written to it. For that use the command syntax, “Command > /dev/null 2>&1”.

What is redirect output to dev null 2 >& 1?
By redirecting output to the /dev/null device, you can suppress any output (represented by the file descriptor- 1) or error messages (represented by the file descriptor- 2). As this is a special device file that suppresses everything written to it. The operator  “2>&1” is used to redirect both standard output & error. For this purpose use the command syntax, ‘Command > /dev/null 2>&1’.

Related Articles


<< Go Back to Bash Output | Bash I/O | Bash Scripting Tutorial

Rate this post
Monira Akter Munny

Hello!! This is Monira Akter Munny. I'm a Linux content developer executive here, at SOFTEKO company. I have completed my B.Sc. in Engineering from Rajshahi University of Engineering & Technology in the Electrical & Electronics department. I'm more of an online gaming person who also loves to read blogs & write. As an open-minded person ready to learn & adapt to new territory, I'm always excited to explore the Linux world & share it with you! Read Full Bio

Leave a Comment