Hack #10-> Suppress Standard Output and Error Message

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.

# cat file.txt > /dev/null

# ./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.

# cat invalid-file-name.txt 2> /dev/null

# ./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.

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

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s