How do I suppress shell script error messages?

ShellSuppress WarningsError Suppression

Shell Problem Overview


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?

Shell Solutions


Solution 1 - Shell

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.

Solution 2 - Shell

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

Solution 3 - Shell

We can use 2> /dev/null to suppress the output error and || true to ensure a success exit status:

rm foo
=> rm: cannot remove ‘foo’: No such file or directory

rm foo 2> /dev/null
echo $?
=> 1

rm foo 2> /dev/null || true
echo $?
=> 0

If you are using the command in a shell script, makefile, etc, maybe you need this.

Solution 4 - Shell

you should redirect all error message to /dev/null like

rm tempfl2.txt 2> /dev/null

Solution 5 - Shell

Adding to the answers above: It is probably a better idea to keep error messages (like permission denied or some such). Just test existence of the file before deleting it:

[ -f file.txt ] && rm file.txt

This assumes a Bourne like shell, e.g., bash. The above has the additional benefit that it won't try to delete a directory, something rm can't do.

Solution 6 - Shell

Try this command:

rm -f tempfl.txt

the -f option acts like this:

-f, --force  ignore nonexistent files, never prompt

The command also doesn't report a non-zero error code in case the file doesn't exist.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohn Andreas WestmanView Question on Stackoverflow
Solution 1 - ShellkamituelView Answer on Stackoverflow
Solution 2 - ShelldavnicwilView Answer on Stackoverflow
Solution 3 - ShellFernando AlmeidaView Answer on Stackoverflow
Solution 4 - ShelldeveloperView Answer on Stackoverflow
Solution 5 - ShellmplworkView Answer on Stackoverflow
Solution 6 - ShellPierluigi VernettoView Answer on Stackoverflow