For example, I have a file:
$ cat test.file
a
b
c
If I grep
with output, I got echo $?
to 0
:
$ grep a test.file
a
$ echo $?
0
But if I grep
without output, I got echo $?
to 1
$ grep x test.file
$ echo $?
1
Because I got $?
to 1
, my pipeline with this command is failed.
What should I do to get consistent result as 0
, since grep
has no output is fine to me.
Add
|| true
to the end of the statement, for example:I prefer the above approach, since it is more explicit and more concise than setting/resettings
errexit
orpipefail
like so:This is especially true if you have multiple
grep
commands scattered throughout the script.