suppress shell script error messages

前端 未结 6 1264
深忆病人
深忆病人 2020-12-24 10:30

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: temp         


        
6条回答
  •  生来不讨喜
    2020-12-24 11:06

    As the other answers state, you can use command 2> /dev/null to throw away the error output from command

    But what is going on here?

    > is the operator used to redirect output. 2 is a reference to the standard error output stream, i.e. 2> = redirect error output.

    /dev/null is the 'null device' which just swallows any input provided to it. You can combine the two to effectively throw away output from a command.

    Full reference:

    • > /dev/null throw away stdout
    • 1> /dev/null throw away stdout
    • 2> /dev/null throw away stderr
    • &> /dev/null throw away both stdout and stderr

提交回复
热议问题