grep will exit 1 when no matching could be found. You won't notice this until you run it on CircleCI and the build job exit 1 with no error message at all.

Quote from the man page of grep:

Normally the exit status is 0 if a line is selected,
1 if no lines were selected, and 2 if an error occurred.
However, if the -q or --quiet or --silent is used and a line is selected,
the exit status is 0 even if an error occurred.


(grep $something || true)

To force grep exit 0 when no mathing could be found. This also works with grep -v when you want to find the invert matching result.


If your grep command might be ends with no matching, and you need the output of grep to combine with awk, cut, xargs, ... you should use grep on CircleCI like this:

(grep ... || true)
(grep ... || :)

This () syntax works for bash, sh, zsh

For example:

${COMMAND_WITH_UNSURE_OUTPUT} | (grep ${TARGET_STRING} || true) | cut -d ' ' -f 1 | xargs ...

This will force grep return 0 even if no matching, and you could still pipe the output to another command. Unlike grep -q which returns 0 but no output could be used.


These commands do not have same effect. Might vary from different shells, use at your own risk and make sure you know what you are doing.

(grep $something || true)
{grep $something || true;}

(grep $something || test $? = 1)
{grep $something || test $? = 1;}

(grep $something || [[ $? == 1 ]])
{grep $something || [[ $? == 1 ]];}

(grep $something || :)
{grep $something || :;}

More discussions in here:

Things about CircleCI

You might want to ask: "But, I pipe the output to another command and the result of echo $? is 0."

According to CircleCI uses /bin/bash -eo pipefail for linux job. It will check the exit code of every command between pipes, not just the $? after the whole line of commands exectued.

Exit immediately if a pipeline (which may consist of a single simple command), a subshell command enclosed in parentheses, or one of the commands executed as part of a command list enclosed by braces exits with a non-zero status.

-o pipefail
If pipefail is enabled, the pipeline’s return status is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit successfully. The shell waits for all commands in the pipeline to terminate before returning a value.

Once your grep doesn't find any matching, You will get this error on CircleCI:

Exited with code exit status 1
CircleCI received exit code 1



如果覺得這篇文章對你有幫助, 除了留言讓我知道外, 或許也可以考慮請我喝杯咖啡, 不論金額多寡我都會非常感激且能鼓勵我繼續寫出對你有幫助的文章。

If this blog post happens to be helpful to you, besides of leaving a reply, you may consider buy me a cup of coffee to support me. It would help me write more articles helpful to you in the future and I would really appreciate it.

Related Posts