BASH Shell Redirect Output and Errors To /dev/null

by Vivek Gite on February 11, 2009 · 12 comments

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?

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:

$ command > /dev/null 2>&1
$ ./script.sh > /dev/null 2>&1
$ ./example.pl > /dev/null 2>&1

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

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 12 comments… read them below or add one }

1 sherikrot February 11, 2009

Another way to do it:

$ command &>/dev/null

Reply

2 Giuseppe February 12, 2009

Or you can close stdout and stderr for the command being executed:

$ command 1>&- 2>&-

Reply

3 Jose Torres October 15, 2011

Remember to add an additional & at the end of the statement to run the command in the background. Thank you Giuseppe for the tip.

Reply

4 Jonathan May 26, 2009

Thanks! I was searching how resolve this problem, and your solution work perfect for me!

Reply

5 Frank June 30, 2009

need a command in my bash script to remove some (not all) of the contents of directory2.
The script does NOT run as root, which works because it removes the correct files but not the root-level stuff in directory2 (that I don’t want to remove).
Problem is users get confused by the “permission denied” msgs output by the “rm”. So…
I tried to redirect the stderror & stdout to /dev/null this way:
rm * /directory1/directory2/ > 2&>1 /dev/null
kept changing /dev/null form a special file & other users need crw-rw-rw-
Will the recommended approaches allow me to redirect to /dev/null without messing it up for others?

Reply

6 Henry April 14, 2010

how does one redirect output from text file processing to a script file that uses the command line variable $1.

file iplist has a long list of IP’s on the network and i need to send this to a script that creates a file with the ping info.

script says: ping $1 > $1
Please assist if possible

Reply

7 SilversleevesX July 20, 2010

How reliable, if that’s the word I’m looking for, is ending a particular command in a script with a redirect like “2>/dev/null” ? What have folks’ experiences been with the different commands and bash/sh versions when trying it this way?

I know it’s not recommended, but for someone like myself, with scripts they either run daily or don’t run for months and then go through a spate of executing them two and three times a day (only to go back to seldom running them until the next time it happens), it would be very convenient and not too too anxiety-producing to run a script and know that whatever passable or critical errors it comes up with are being suppressed.

I’m much more inclined to put up with circumstances after the fact, and I seldom write anything that’s too destructive (on the system or OS/hardware install and performance level, at any rate) for a little error like Exiv2 complaining about some JPG file’s Photoshop IFD entry being out of bounds.

So share up, coders and newbies. :)

BZT

Reply

8 Saartube January 19, 2011

Thank you :))

Reply

9 ciccio October 2, 2011

Hi,
how can I redirect output to /dev/null BUT errors on sdout.
I mean: I want to launch a command:
- if all goes good —> no output
- if something goes wrong —> output of errors

Thanks,
Ciccio

Reply

10 SilversleevesX October 2, 2011

ciccio -
I think it would be the opposite of sending errors to the bucket.

Something like:
(your_command) 1>/dev/null
should leave errors alone, that is, going to stout where you can see them. I’m sure you have something in mind where both good and bad output would normally go to stdout.

BZT

Reply

11 josch October 5, 2011

ciccio, the order of the redirection counts.
use:
command 2>&1 1>/dev/null

Reply

12 Vivek Gite October 6, 2011

No, it does not matters. So following two are the same command:

command 2>&1 1>/dev/null

AND

command 1>/dev/null 2>&1

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 12 + 8 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: