Hack 19. Suppress Standard Output and Error Message

by Ramesh

Sometime while debugging a shell script, you may not want to see either the standard output or standard error message. Use /dev/null as shown below for suppressing the output.

Suppress standard output using > /dev/null

This will be very helpful when you are debugging shell scripts, where you don’t want to display the echo statement and interested in only looking at the error messages.

  1. # cat file.txt > /dev/null
  2.  
  3. # ./shell-script.sh > /dev/null

Suppress standard error using 2> /dev/null

This is also helpful when you are interested in viewing only the standard output and don’t want to view the error messages.

  1. # cat invalid-file-name.txt 2> /dev/null
  2.  
  3. # ./shell-script.sh 2> /dev/null

Note: One of the most effective ways to use this is in the crontab, where you can suppress the output and error message of a cron task as shown below.

  1. 30 1 * * * command > /dev/null 2>&1