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?

2 Answers 2

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, 2010 at 16:10
  • 8
    You can also do || true if you're trying to bypass failure under set -e
    – Daenyth
    Jul 20, 2010 at 16:57
  • @Daenyth Of course that transforms every non-zero exit code to 0, not only 1. Mar 9, 2017 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.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .