6

When I need to suppress some kind of output e.g.

foo | grep -v bar

But foo gives sometimes only:

bar
bar
bar...

grep will return -1. How do I transform the exit code 1 to 0?

8

You could always try piping it through something else, like cat, to get rid of the exit code from grep - though that may be a bigger hammer than you want.

3
  • and this seems even to be Posix compliant – math Jul 20 '10 at 16:10
  • 8
    You can also do || true if you're trying to bypass failure under set -e – Daenyth Jul 20 '10 at 16:57
  • @Daenyth Of course that transforms every non-zero exit code to 0, not only 1. – MarnixKlooster ReinstateMonica Mar 9 '17 at 9:54
1

At least in bash, you can use

foo || (EC=$?; if [ $EC -ne 1 ]; then exit $EC; fi)

This changes exit code 1 to 0, and changes nothing else.

(Perhaps not POSIX-compliant, but probably a POSIX-compliant version exists.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.