grep returns a different exit code if its found something (zero) vs. if it hasn't found something (non-zero). In an if statement, a zero exit code is mapped to "true" and a non-zero exit code is mapped to false. In addition, grep has a -q argument to not output the matched text (but only return the exit status code)

So, you can use grep like this:

if grep -q PATTERN file.txt; then
    echo found
else
    echo not found
fi


As a quick note, when you do something like if [ -z "$var" ]…, it turns out that [ is actually a command you're running, just like grep. On my system, it's /usr/bin/[. (Well, technically, your shell probably has it built-in, but that's an optimization. It behaves as if it were a command). It works the same way, [ returns a zero exit code for true, a non-zero exit code for false. (test is the same thing as [, except for the closing ])

Can grep return true/false or are there alternative methods

  One would never defeat one’s circumstances by working and saving one’s
  pennies; one would never, by working, acquire that many pennies, and,
  besides, the social treatment accorded even the most successful
  Negroes proved that one needed, in order to be free, something more
  than a bank account.


Letter from a Region in My Mind

Can I understand the bold word as "accorded that"?

Thank you.
