Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 740
John Andreas Westman Avatar asked Mar 28 '13 09:03

John Andreas Westman


People also ask

How do I ignore standard error?

You can ignore standard error on screen when you write stderr to /dev/null .

How do I turn off output in bash?

To silence the output of a command, we redirect either stdout or stderr — or both — to /dev/null. To select which stream to redirect, we need to provide the FD number to the redirection operator.


2 Answers

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.

like image 136
kamituel Avatar answered Nov 09 '22 17:11

kamituel


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
like image 21
davnicwil Avatar answered Nov 09 '22 18:11

davnicwil