When you run:
ping -q -c 1 google.com > /dev/null && echo online || echo offline
You are essentially only redirecting the output of Stream 1 (i.e. stdout
) to /dev/null
.
This is fine when you want to redirect the output that is produced by the normal execution of a program. However, in case you also wish to redirect the output caused by all the errors, warnings or failures, you should also redirect the stderr
or Standard Error stream to /dev/null
.
One way of doing this is prepending the number of the stream you wish to redirect to the redirection operator, >
like this: Command 2> /dev/null
Hence, your command would look like:
ping -q -c 1 google.com > /dev/null 2> /dev/null && echo online || echo offline
But, notice that we have already redirected one stream to /dev/null
. Why not simply piggyback on the same redirection? Bash allows us to do this by specifying the stream number to which to redirect to. 2>&1
.
Notice the &
character after the redirection operator. This tells the shell that what appears next is not a filename, but an identifier for the output stream.
ping -q -c 1 google.com > /dev/null 2>&1 echo online || echo offline
Be careful with the redirection operators, their order matters a lot. If you were to redirect in the wrong order, you'll end up with unexpected results.
Another way in which you can attain complete silence is by redirecting all output streams to /dev/null
using this shortcut: &>/dev/null
(or redirect to a log file with &>/path/to/file.log
).
Hence, write your command as:
ping -q -c 1 google.com &> /dev/null && echo online || echo offline
time
command than manually computing time difference withdate
:time { fing -p googleadf.com > /dev/null 2>&1 && echo online || echo offline; }
– Ruslan Jan 29 '14 at 11:44date
. It's easier, IMO, for someone else to read the output generated. – slm Jan 29 '14 at 13:22