redirect output of command to /dev/null

16,852

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.

Share:
16,852

Related videos on Youtube

yael
Author by

yael

Updated on September 18, 2022

Comments

  • yael
    yael over 1 year

    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?

    • Petro
      Petro almost 8 years
      Stdio (standard out) is 1 (in this context) stderr is 2. I suspect you want to redirect stderr, meaning 2>/dev/null As to where, I'm not exactly sure what you're doing in that line so I would rather not hazard a guess.