Com a opção -F , grep procura correspondências exatas (recursos de regex desativados):
$ grep -F "defabc" xyz.txt
defabc
grep define um código de retorno apropriado para que possamos testar verdadeiro ou falso:
$ if grep -qF "defabc" xyz.txt; then echo True; else echo False; fi
True
$ if grep -qF "Defabc" xyz.txt; then echo True; else echo False; fi
False
Como só queremos que grep defina um código de retorno, a opção -q é usada para informar grep para ficar quieto (omitir a saída normal).
Restringindo a correspondência a palavras inteiras
Se quisermos corresponder defabc , mas não defabc1 , podemos adicionar a opção -w para restringir a correspondência a palavras inteiras:
if grep -qFw "defabc" xyz.txt; then echo True; else echo False; fi
man grep explica como grep define uma "palavra":
-w, --word-regexp
Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character.
Word-constituent characters are letters, digits, and the underscore.
Capturando a saída em uma variável do shell
Para capturar a saída de um comando, usamos substituição de comando , que assume o formato $(...) :
$ r=$(if grep -qF "defabc" xyz.txt; then echo True; else echo False; fi)
$ echo $r
True
Capturando o código de saída em uma variável do shell
grep -qF "defabc" xyz.txt
r=$?
r será zero se houver uma correspondência, 1 se não houver ou 2 se algum outro erro ocorrer.