Use the -q
flag for quiet mode. In command line world, “quiet” usually means “don’t print stuff to the screen”. This may seem weird since so many of the examples we have seen so far show results to the screen. How do we know if the pattern was found? We can do so by inspecting the exit status.
Let’s return to our test.txt
file with the following content:
sad
happy
awake
coffee
work
school
Let’s search for “happy” with the -q
flag and then look at the exit status:
$ grep -q "happy" test.txt
$ echo $?
0
An exit status of 0 means success. It’s also important to note that the grep command is exiting immediately after finding the first occurrence of “happy” and it will not continue.
Let’s try again with a term that is not in test.txt
:
$ grep -q "bright" test.txt
$ echo $?
1
An exit status of 1 means that the term was not found in the file.