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

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:

Steps to Follow >

➊ At first, launch your Ubuntu Terminal application.

➋ Now, write the following command to open a file in the nano text editor:


Open .sh file in the nano editor➌ Afterward, write the following script in the nano editor:

#! /bin/bash

if ls “$file” &> /dev/null; then #Checks files list & redirects output & error messages to /dev/null
   echo “File exists”
   echo “File does not exist.”

Here, #! /bin/bash: ‘#!’, is called shebang or hashbang. It indicates the interpreter to be used for executing the script, in this case, it’s bash. Next, the ‘file’ variable is assigned the path to the file we want 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.

➍ 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

Add execute permission to a Bash script

  • 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:


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

In this bash script example, while using a for loop, I will suppress only the error messages but will display the script output messages.

You can follow the steps of Example 01, to save & make the script executable.

Script ( >

#! /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.”
    echo “Directory ‘$dir’ does not exist.”

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 command print the quoted messages if exist then ‘exists’ & if not then ‘does not exist’.

Now, run the script by the following command:


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

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.

You can follow the steps of Example 01, to save & make the script executable.

Script ( >

#! /bin/bash

if curl -s “$url” > /dev/null; then #Checks the URL request validity
   echo “Request succeeded”
   echo “Request failed”

Here, the script uses the ‘curl’ command to make an HTTP request to the specified URL”. 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”.

Now, run the script by the following command:


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.


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