How do I suppress shell script error messages?
Asked Answered
R

6

87

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?

Rations answered 28/3, 2013 at 9:57 Comment(1)
Have a look here: #12786033Describe
A
103

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.

Ashcroft answered 28/3, 2013 at 9:59 Comment(1)
rm -f should be used carefully.Gnosis
E
109

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
Eichelberger answered 26/6, 2018 at 14:26 Comment(3)
Thanks. This is the first place I've seen fully explain what the different parts of 2&>1 actually mean.Brilliancy
@Brilliancy So what does 2&>1 do? Isn't the 2 redundant here?Alysiaalyson
Please resist using &>/dev/null which appears to be a shorthand of > /dev/null 2>&1! It might not be harmful in Bash, but to clear things up: It's not POSIX compliant and always produces an exit code of 0, when using #!/usr/bin/env sh as a shebang on some systems. It's a better habit to use > /dev/null 2>&1 instead. I had to learn it the hard way.Partition
A
103

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.

Ashcroft answered 28/3, 2013 at 9:59 Comment(1)
rm -f should be used carefully.Gnosis
S
11

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.

Slicer answered 27/12, 2017 at 18:28 Comment(0)
O
8

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

rm tempfl2.txt 2> /dev/null
Otha answered 28/3, 2013 at 9:59 Comment(0)
P
8

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.

Polarization answered 28/3, 2013 at 10:9 Comment(1)
This is obviously prone to error in scenarios where something else removes the file between the time you check and the time you attempt to remove it. rm -f file.txt is atomic and will simply no-op if the file doesn't exist.Rhodonite
T
6

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.

Thrive answered 19/2, 2014 at 14:50 Comment(1)
Could you give more of an explanation for your answer?Kino

© 2022 - 2024 — McMap. All rights reserved.