Use -q
:
if grep -q "$item" filename; then
echo "it exists"
else
echo "does not exist"
fi
Or in a one liner:
grep -q "$item" filename && echo "it exists" || echo "does not exist"
From man grep
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit immediately
with zero status if any match is found, even if an error was detected.
Also see the -s or --no-messages option. (-q is specified by POSIX.)
As Adrian Frühwirth points below, grep -q
alone will just silence the STDIN. If you want to get rid of the STDERR, you can redirect it to /dev/null
:
grep -q foo file 2>/dev/null
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…