Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have the grep command. I'm searching for a keyword from a file, but I don't want to display the match. I just want to know the exit status of the grep.

share|improve this question

4 Answers 4

up vote 6 down vote accepted

If you're using GNU grep it has the switch -q for quiet.

-q, --quiet, --silent     suppress all normal output

Other versions of grep that are POSIX compliant will also have this switch too.

Example

String exists:

$ echo "here" | grep -q "here"
$ echo $?
0

String doesn't exist:

$ echo "here" | grep -q "not here"
$ echo $?
1
share|improve this answer
4  
The '-q' option is also found in non-GNU versions of grep. –  DarkHeart Dec 11 '13 at 2:57
3  
To expand further on @DarkHeart's comment, -q is part of POSIX. –  Chris Down Dec 11 '13 at 4:01
    
@ChrisDown - thanks, I've incorporated that fact into the OS. –  slm Dec 11 '13 at 4:13

Just redirect output of grep to /dev/null:

grep sample test.txt > /dev/null

echo $?
share|improve this answer
    
This will fail to echo $? if grep returns a non-zero exit code. –  WinnieNicklaus Dec 11 '13 at 5:00
    
Thanks, fixed it. –  cuonglm Dec 11 '13 at 5:52

You simply need to combine grep -q <pattern> with an immediate check of the exit code for last process to quit ($?).

You can use this to build up a command like this, for example:

uname -a | grep -qi 'linux' ; case "$?" in "0") echo "match" ;; "1") echo "no match" ;; *) echo "error" ;; esac

You can optionally suppress output from STDERR like so:

grep -qi 'root' /etc/shadow &> /dev/null ; case "$?" in "0") echo "match" ;; "1") echo "no match" ;; *) echo "error: $?" ;; esac

This will print error: 2 from the case statement (assuming we do not have privileges to read /etc/shadow or that the file does not exist) but the error message from grep will be redirected to /dev/null so we don't ever see it.

share|improve this answer

If you don't want to see any output, you need to redirect both stderr and stdout to /dev/null. The other answers are good but won't suppress errors, such as trying to read from a non-existent or unreadable (e.g. wrong permissions) file. Append &> /dev/null (works in bash and some other shells, but not all; >/dev/null 2>&1 may be more portable) so you won't see any output.

$ ls thisfiledoesnotexist
ls: cannot access thisfiledoesnotexist: No such file or directory
$ grep foo thisfiledoesnotexist
grep: thisfiledoesnotexist: No such file or directory
$ grep -q foo thisfiledoesnotexist
grep: thisfiledoesnotexist: No such file or directory
$ grep foo thisfiledoesnotexist >/dev/null
grep: thisfiledoesnotexist: No such file or directory
$ grep foo thisfiledoesnotexist &>/dev/null
$ echo $?
2
$ touch nopermissions
$ chmod 000 nopermissions
$ grep -q foo nopermissions
grep: nopermissions: Permission denied
$ grep foo nopermissions > /dev/null
grep: nopermissions: Permission denied
$ grep foo nopermissions &> /dev/null
$ echo $?
2
$ 

If you've already verified that the target file exists and is a file and you have permissions to read it, this isn't that important, but if you're being a little more careless and grepping recursively or using wildcards, you're more likely to see output on stderr.

share|improve this answer
2  
Redirections are always processed left to right, 2>&1 >/dev/null should be >/dev/null 2>&1. –  Chris Down Dec 11 '13 at 7:30
    
I didn't realize that. Thank you @ChrisDown. Fixed. –  drewbenn Dec 11 '13 at 7:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.