Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to write this script for an assignment - it's only the second one I've written so bear with me.

As a part of this script, I need to be able to check if the first argument given matches the first word of file. If it does, exit with an error message, if it doesn't, append the arguments to the file. I understand how to write the if statement, but not how to use grep within a script. I understand that grep will look something like this

grep ^$1 schemas.txt

I feel like this should be much easier than I am making it. Any help appreciated.

Update: I'm getting an error too many arguments on the if statement. I got rid of the space between grep-q and then got an error binary operator expected. I've been poking around at it, but I don't see what it sees.

if [ grep -q ^$1 schemas.txt ]
        echo "Schema already exists. Please try again"
        exit 1
        echo "$@" >> schemas.txt
share|improve this question
Lose the [] and it'll work. Though you probably want to quote your pattern: if grep -q "^$1" schemas.txt; then … – derobert Sep 18 '12 at 19:25
up vote 57 down vote accepted

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
    echo not found

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 ])

share|improve this answer
Why doesn't the if statement need the brackets? I have it working without, but don't understand why. Can I still nest it without the brackets? – Lauren Sep 18 '12 at 18:47
@Lauren did you miss the quick note? [ is not part of the if syntax, it's (conceptually) a command you're running, just like grep – derobert Sep 18 '12 at 19:06
Ok. I read it, just having trouble understanding it. So, since grep -q itself is returning a true/false value, it is unnecessary and problematic to use the command [ also? – Lauren Sep 18 '12 at 19:19
@Lauren You don't use grep inside of [, you use one or the other, depending on what condition you want to check. (You can use any command inside an if, btw, if just checks exit code.) … well, I guess you probably could come up with a reason to use grep inside [, but that'd be a fairly complicated script, and its not a normal thing to do. – derobert Sep 18 '12 at 19:21
Ok. Thanks so much for your patience with my semi-redundant questions. – Lauren Sep 18 '12 at 19:30

Another simple way is to use grep -c.

That outputs (not return as exit code), the number of lines that match the pattern, so 0 if there's no match or 1 or more if there's a match.

So, if you wanted to check that the pattern is matched 3 or more times, you would do:

if [ "$(grep -c "^$1" schemas.txt)" -ge 3 ]; then
share|improve this answer
More precisely, it will output 1 if found only once. To output 1 regardless the amount of found matches use grep -cim1 instead. – manatwork Apr 29 '13 at 10:26
thanks for the correction – amigal Apr 29 '13 at 11:34

Try ( ) instead of [ ], will work. Even if you don't use any brackets, code will work.

share|improve this answer
using ( ) merely fork a new shell in which condition is executed. – Archemar Dec 31 '15 at 9:51

Your Answer


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.