logo
Published on

Grep

Authors
  • avatar
    Name
    Bowen Y
    Twitter

grep failed with exit code 1 in CircleCI workflow

  1. grep will throw an error when nothing matched
$ echo "anything" | grep a
a
$ echo $?
0
$ echo "anything" | grep b

$ echo $?
1

This command behaves the same across different system environments.

How to make grep return 0 if nothing matched?

Add || true. If the first part of the command "fails" (meaning grep e returns a non-zero exit code) then the part after the || is executed, succeeds and returns zero as the exit code (true always returns zero).

$ echo "anything" | grep b || true

$ echo $?
0
  1. Can we use | wc -l to decide whether anything is matched instead of using || true?

Typically, we can. I tried on local machine as well as CircleCI SSH server CLI.

# On local machine OR CircleCI SSH server
$ echo "anything" | grep b | wc -l
0
$ echo $?
0

However, in the CircleCI automation workflow, it will raise error code 1 too.

# In CircleCI CICD workflow
$ echo "anything" | grep b | wc -l
0

Exited with code exit status 1