How do I suppress shell script error messages?

John Andreas Westman picture John Andreas Westman · Mar 28, 2013 · Viewed 90.8k times · Source

In my shell script I got these lines:

rm tempfl.txt
rm tempfl2.txt

If these do not exist I get the error messages:

rm: tempfl2.txt: No such file or directory
rm: tempfl.txt: No such file or directory

Is there a way to only suppress these messages even though they do not always appear, as the files might exist?

Answer

kamituel picture kamituel · Mar 28, 2013

You have two options:

Suppress rm warnings

$ rm tempfl.txt 2> /dev/null

Redirect script output to /dev/null

$ ./myscript.sh 2> /dev/null

The latter has a drawback of missing all other warning messages produced by your script.