Shell Script – How to Redirect Output to /dev/null

io-redirectionperlshell-scriptssh

I run the following line from my bash script in order to run the script /usr/run.pl on remote machines

  xargs -P "$numprocs" -I '{}' -n  1 -- perl -e 'alarm shift; exec @ARGV' -- "$timeout" ssh -nxaq -o ConnectTimeout=5 -o StrictHostKeyChecking=no '{}' /usr/run.sh < <(echo -e "$node") 

but I get on the console the following standard output

Connection to 143.6.22.4 closed by remote host.
 xargs: perl: exited with status 255; aborting

where I need to put the 1>/dev/null in my syntax to avoid this message?

Best Answer

There are two streams of output from a process that are generally sent to the terminal: standard output (which is bound to file descriptor 1), and standard error (which is bound to file descriptor 2). This allows for easily separated capture of expected output, and error messages or other diagnostic information that is not generally expected to be output.

When you use the redirect (>), that only kicks standard output to the specified file or location, leaving standard error untouched. This allows you to see if there are errors (like those you are seeing).

If you want to send all output, including standard error, you need to redirect both streams:

/path/to/program arg1 arg2 > /dev/null 2>&1

Or, alternatively but more explicitly:

/path/to/program arg1 arg2 > /dev/null 2> /dev/null

The syntax 2>&1 means "Send output currently going to file descriptor 2 to the same place that output going to file descriptor 1 is going to". > is omitting the default of FD1, so it is semantically the same as 1>, which might make 2> make more sense.

Related Question