How can I hide the screen output (printf) of a shell application in Linux?
5 Answers
You can redirect the output of any program so that it won't be seen.
$ program > /dev/null
This will redirect the standard output - you'll still see any errors
$ program &> /dev/null
This will redirect all output, including errors.
-
The first one didn't work for wgetJader Dias– Jader Dias2009-07-17 03:07:45 +00:00Commented Jul 17, 2009 at 3:07
-
That's cause wget uses stderr for some of it's output. The second one should work.theotherreceive– theotherreceive2009-07-17 03:08:47 +00:00Commented Jul 17, 2009 at 3:08
-
2Incidentally, you might want to save that wget output to a log file, so when/if your download stops working, you can figure out why. If it's in a script anyway. If this is a one-off type run, then yea, to the trashMatt Simmons– Matt Simmons2009-07-17 03:17:51 +00:00Commented Jul 17, 2009 at 3:17
-
1For
wgetyou can use the-qoptions to make it quiet.pkhamre– pkhamre2012-03-30 10:12:21 +00:00Commented Mar 30, 2012 at 10:12 -
Any way to suppress output written directly to /dev/tty?d11wtq– d11wtq2014-07-17 07:08:51 +00:00Commented Jul 17, 2014 at 7:08
There are three I/O devices available on the command line.
standard input - 0
standard output - 1
standard error - 2
To redirect standard output (the default output) to a file (and overwrite the file), use
command > file.log
To append to file.log, use two >s
command >> file.log
To redirect standard error to the file.log, use
command 2> file.log
And to append
command 2>> file.log
To combine the outputs into one stream and send them all to one place
command > file.log 2>&1
This sends 2 (standard error) into 1 (standard output), and sends standard output to file.log
Notice that it's also possible to redirect standard input into a command that expects standard input
command << file.txt
For more details, check out the Advanced Bash Scripting Guide.
-
Can somebody explain how the
command > file.log 2>&1works?Cory Klein– Cory Klein2011-05-23 22:37:53 +00:00Commented May 23, 2011 at 22:37 -
How low of a level would you like to know?Matt Simmons– Matt Simmons2011-05-29 13:54:50 +00:00Commented May 29, 2011 at 13:54
-
4@nomoreink: it's actually 2 commands, one is
> fileand the second one is2>&1. The first one redirects the standard out to a file. The second one takes 2nd file descriptor and redirects it to first one. You can do the reverse, redirect standard output to standard error using>&2and then redirect standard error to a file with2> file.Alicja Kario– Alicja Kario2012-11-27 11:39:22 +00:00Commented Nov 27, 2012 at 11:39
Hide standard output:
./command >/dev/null
Hide standard and error outputs:
./command >/dev/null 2>&1
Hide standard output and error outputs and release the terminal (run the command in the background):
./command >/dev/null 2>&1 &
If you just want to hide the output (and not save it to a file), you can use:
Edited:
$ command &> /dev/null
-
This will redirect the output to a file called nulltheotherreceive– theotherreceive2009-07-17 03:03:35 +00:00Commented Jul 17, 2009 at 3:03
-
It generated a null fileJader Dias– Jader Dias2009-07-17 03:04:34 +00:00Commented Jul 17, 2009 at 3:04
-
1you wouldn't have meant /dev/null, would you?Babu– Babu2009-07-17 03:09:15 +00:00Commented Jul 17, 2009 at 3:09
-
1Yes, I would have, Babu. I meant $ command &> /dev/null. My apologies for typing too fast for my own good.Lucho– Lucho2009-07-17 03:13:13 +00:00Commented Jul 17, 2009 at 3:13
For Mac OS X v10.6 (Snow Leopard):
If you need to hide the output without letting the program know it by checking the output/error file descriptor, you can try using the following in a shell:
stty flusho; command ;stty -flusho
or if you just want to hide input from the terminal by the way:
stty -echo; command ;stty echo
See stty(1) manual page for more information.
For Linux, all I know is that Ubuntu 10.04 (Lucid Lynx) and some Debian/Arch Linux (commented below - thanks, hendry) doesn't have the flusho setting (and I can't see anything other appropriate in the man-page). The echo setting works on the Ubuntu anyway.