Terminal. Why is the exit grep command set to 0, even if no match is found?

I have this command:

grep -E '^nothing' List.txt | echo $?

Here grep doesn't match anything and I just output its exit code. According to grep documentation:

Typically, the exit status is 0 if a line is selected, 1 if no lines were selected, and 2 if an error occurred. However, if -q or --quiet or --silent, and a string is selected, the exit status is 0, even if an error occurs. Other grep implementations may exit with status greater than 2 on error.

But:

prompt:user$ grep -E '^nothing' List.txt | echo $?
0
prompt:user$

But why do I get 0 as output, even if the match does not exist, should I not get the expected exit code 1?

+4
source share
1 answer

This is the problem:

grep -E '^nothing' List.txt | echo $?

|, grep echo, , 0, .

grep -q:

grep -qE '^nothing' List.txt

man grep:

 -q, --quiet, --silent
         Quiet mode: suppress normal output.  grep will only search a file until a match
         has been found, making searches potentially less expensive.
+3

All Articles