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?

math
  • 2,633
  • 8
  • 31
  • 43

2 Answers2

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.

Goyuix
  • 6,511
  • 4
  • 37
  • 48
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.)