shellsuppress-warningserror-suppression

How do I suppress shell script error messages?


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?


Solution

  • 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.