Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I suppress error messages of a command?

Tags:

linux

bash

How can I suppress error messages for a shell command?

For example, if there are only jpg files in a directory, running ls *.zip gives an error message:

   $ ls *.zip    ls: cannot access '*.zip': No such file or directory 

Is there an option to suppress such error messages? I want to use this command in a Bash script, but I want to hide all errors.

like image 286
Peter Avatar asked Sep 03 '15 15:09

Peter


People also ask

How do I hide errors in Linux?

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

How do you suppress command output?

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.

How do you stop error messages in bash script?

If you are looking to suppress or hide all the output of a bash shell script from Linux command line as well as from the crontab then you can simply redirect all the output to a file known as /dev/null . This file is known as Black Hole which will engulf everything you give without complaining.

How do I supress errors in Mathematica?

For an Entire Session... To suppress a particular error message for the remainder of the session, click the "More information" icon to the left of the message, choose Turn Off This Message, and click OK: Subsequent evaluations will not produce the message: Copy to clipboard.


2 Answers

Most Unix commands, including ls, will write regular output to standard output and error messages to standard error, so you can use Bash redirection to throw away the error messages while leaving the regular output in place:

ls *.zip 2> /dev/null 
like image 193
AJefferiss Avatar answered Sep 24 '22 08:09

AJefferiss


$ ls *.zip 2>/dev/null 

will redirect any error messages on stderr to /dev/null (i.e. you won't see them)

Note the return value (given by $?) will still reflect that an error occurred.

like image 32
Brian Agnew Avatar answered Sep 23 '22 08:09

Brian Agnew