BASH Shell Redirect Output and Errors To /dev/null

See all UNIX related articles/faq
How do I redirect output and errors to /dev/null under bash / sh shell scripting? How do I redirect the output of stderr to stdout, and then redirect this combined output to /dev/null device? In Unix, how do I redirect error messages to /dev/null?

You can send output to /dev/null, by using command >/dev/null syntax. However, this will not work when command will use the standard error (FD # 2). So you need to modify >/dev/null as follows to redirect both output and errors to /dev/null.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux or Unix terminal
Category Linux shell scripting
OS compatibility BSD Linux macOS Unix
Est. reading time 5 minutes

Understanding Bash file names associated with each process

The following files are used by each Linux or Unix process. Here is how to view them using the ls command:
$ ls -l /dev/{stdout,stdin,stderr}

lrwxrwxrwx 1 root root 15 Jun 10 17:56 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root 15 Jun 10 17:56 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root 15 Jun 10 17:56 /dev/stdout -> /proc/self/fd/1


  1. /dev/stdin (0) – Standard Input (usually keyboard or file). When a command opens /dev/stdin, it can read input from the user.
  2. /dev/stdout (1) – Standard Output (usually screen). When a command writes to /dev/stdout, its output is displayed on the screen.
  3. /dev/stderr (2) – Standard Error (usually screen). When a program or command writes to /dev/stderr, its error messages are displayed on the screen.

These special files can be used to redirect input and output from programs, scripts, and commands. For example:

# Redirect the standard input of the command1 from the file to /dev/stdin:
command1 filename </dev/stdin
# Redirect the standard output of the command2 to the file /tmp/output.txt #
command2 >/tmp/output.txt
# Redirect both the standard output (stdout) and standard error (stderr) of the command3 to the file /tmp/mylog.txt:
command3 2>&1 >/tmp/mylog.txt
TIP: To avoid errors, please ensure there is no black space between > and &. Use >& to ensure they are together.

What is a /dev/null file in Linux and Unix?

BASH Shell Redirect Output and Errors To /dev/null

If you are using a Linux or Unix operating system, you can use a special file called /dev/null to discard any data you write. This file is mostly used to hide the output or errors of programs. For instance, you can use this command to hide the output of either “pwd” or “date“:
$ pwd
$ date
$ pwd >/dev/null
$ date >/dev/null

How does it work?

The > operator redirects the standard output of the “pwd” or “date” command to the file named /dev/null. In other words, the output of the “pwd” or “date” will be discarded and will not be displayed on the screen (stdout). Please note that /dev/null can also be used to read input from a program. For example, the following command will read input from the user and then discard it:
$ command1 </dev/null
The < operator redirects the standard input (stdin) of the command1 to the file /dev/null. This means that the user’s input will be discarded and will not be stored in a variable. The </dev/null provides empty input to the script or command. For example, in this example, I am using the echo command to create the text body of the email, pipes it to the mail command, and sends it to the specified email address named The </dev/null ensures that the “mail” command does not wait for further input:
$ echo 'Test mail body' | mail -s 'Test subject' -r </dev/null

Syntax to redirect error and output messages to /dev/null

The syntax discussed below works with Bourne-like shells, such as sh, ksh, and bash:

command >/dev/null 2>&1
./ >/dev/null 2>&1
./ >/dev/null 2>&1


command &>/dev/null
job arg1 arg2 &>/dev/null
/path/to/script arg1 &>/dev/null

You can also use the same syntax for all your cronjobs to avoid emails and output / error messages:
@hourly /scripts/backup/nas.backup >/dev/null 2>&1
@hourly /scripts/backup/nas.backup &>/dev/null

Redirect both standard error and standard out messages to a log file

You can always redirect both standard error (stdin) and standard out (stdout) text to an output file or a log file by typing the following command:

command >file 2>&1
/path/to/my/cool/appname >myapp.log 2>&1

Want to close stdout and stderr for the command being executed on a Linux/Unix/BSD/macOS (OSX) bash shell?

Try the following syntax:

## Close stdout and stderr for the command being executed ##
## Thanks ##
/path/to/command 1>&- 2>&-
## Note: additional '&' at the end of job to put it in backgrounds ##
job1 1>&- 2>&-  &
/path/to/command 1>&- 2>&-  &

Summing up

The /dev/null is a useful tool that can help you manage the input and output of your scripts and commands under Linux, macOS, BSD, and Unix-like systems. You can write more effective shell scripts and commands by understanding how it works. Here are some additional ways you can use /dev/null:

  • Stop a program from producing error messages on screen.
  • Hide excessive errors or output messages sent to the screen by any commands or scripts.
  • Test the output of a script without showing it.
  • Create a “black hole” to discard data you don’t need.

This page explained BASH, KSH, and other shell redirecting output to /dev/null. For more info, see the following manual pages using the help command or man command or info command:
$ man bash
$ info bash
$ man ksh

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

31 comments… add one
  • smilyface Oct 8, 2012 @ 14:04

    echo “open 8080″| telnet | grep –color=auto “Connected to”
    gives the following output:
    Connection closed by foreign host.
    Connected to (

    How can I get rid of “Connection closed by foreign host.” ?

    • neonatus Oct 17, 2012 @ 19:29


      you can close (omit) the stderr output from telnet command
      echo “open 8080″| telnet 2>&- | grep –color=auto “Connected to”

  • siva Sep 13, 2013 @ 6:21


    I tried like below

    ping > /dev/null 2>&1
    “but i am getting out as ” Ambiguous output redirect”
    Please suggest me

    • ap Apr 26, 2014 @ 13:14

      Put in bash script:
      exec 2>/dev/null
      before your commands. And avoid redirection in the command itself.

  • Hugues Nov 12, 2013 @ 16:33

    l often do the following and I do not want an error (just a 0 length file)
    You get a valid output if the command works, otherwise the error is sent to /dev/null

    file=`ls doesthisfileexist 2>/dev/null`
    if [ -n $file ] ; then
    do something

    • LiPi Mar 4, 2014 @ 17:40

      Why not:

      if [ ! -f $FILE ]
          echo "File does not exist"
  • Tom Dec 27, 2014 @ 18:20

    I just stumbled upon this article… FYI ‘command > /dev/null 2>&1` won’t work in every scenario. For example, this will still output an error message:

    ps -ef | grep | grep ps > /dev/null 2>&1

    • 🛡️ Vivek Gite (Author and Admin) nixCraft Feb 2, 2015 @ 20:14


      (ps -ef | grep | grep ps) > /dev/null 2>&1
      • Eric Sebasta May 15, 2015 @ 15:04

        That is a pretty slick little trick. Didn’t know that one. Thanks!

  • ma thesh Feb 2, 2015 @ 18:16

    How to get the error help in shell window

  • Alex Oct 19, 2015 @ 10:02

    Exactly what i wanted!

  • Shyam Nov 18, 2015 @ 16:10


    Please tell me how to redirect the output from a script to a log file so that i can save all the details which i am capturing in the script using read command.

    Here is a snippet of my code:
    echo “Enter the number”
    read $N > text

    If i open text in vi i am getting blank lines and i have saved my script as and done chmod on the script to give it user permissions as well.

    • Anonymous Sep 21, 2020 @ 11:26

      Add the following at the top of your script:

      exec 3>&1 4>&2
      trap 'exec 2>&4 1>&3' 0 1 2 3
      exec 1>/path/to/log.txt 2>&1


      ## Everything done by this script here is logged into /path/to/log.txt
      exec 3>&1 4>&2
      trap 'exec 2>&4 1>&3' 0 1 2 3
      exec 1>/path/to/log.txt 2>&1
      ## add rest of script below
      echo "Adding user..."
      adduser foo
      echo "Setting up keys ..."
  • Kevin Sullivan Sep 29, 2020 @ 20:51

    FYI to use standard out AND error out in a bash script, you cannot have a space between > and &. It’ll give you some wacky error. Just like written on his page, be sure to use >& so that they are together. I just figured this out after battling with the error I previously mentioned. Good luck!

  • Shooler Jul 15, 2022 @ 11:20
    echo "I knew everything but the '&-' notation, very helpful, will be using it very often from now on!" 2>&-
  • Jane Elizabeth Smith Jun 14, 2023 @ 22:02

    This is very useful and all of the examples are easy to follow. Thank you very much.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.