Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash ignoring error for a particular command

Tags:

linux

bash

I am using following options

set -o pipefail set -e 

In bash script to stop execution on error. I have ~100 lines of script executing and I don't want to check return code of every line in the script.

But for one particular command, I want to ignore the error. How can I do that?

like image 905
Vivek Goel Avatar asked Jun 27 '12 17:06

Vivek Goel


People also ask

How do I ignore standard error?

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


1 Answers

The solution:

particular_script || true 

Example:

$ cat /tmp/1.sh particular_script() {     false }  set -e  echo one particular_script || true echo two particular_script echo three  $ bash /tmp/1.sh one two 

three will be never printed.

Also, I want to add that when pipefail is on, it is enough for shell to think that the entire pipe has non-zero exit code when one of commands in the pipe has non-zero exit code (with pipefail off it must the last one).

$ set -o pipefail $ false | true ; echo $? 1 $ set +o pipefail $ false | true ; echo $? 0 
like image 109
Igor Chubin Avatar answered Sep 27 '22 21:09

Igor Chubin