Kippet
Kippet

Reputation: 169

grep command running in if statement

if ! grep $1 'dict'; then
firstword=$1
firstletter=${firstword:0:1};
echo $firstletter

else
    echo "hello"
fi

I'm not sure if it's grep that's running, but when the case is that the word Now is in the file dict it prints the word Now to the standard output. If I put in Noww and Noww isn't in it, it will just print out N. What's causing it to print out Now?

Upvotes: 0

Views: 41

Answers (1)

rici
rici

Reputation: 241931

What's causing it to print out "Now"?

grep is printing out Now, because that's what grep does by default: it prints out input lines which match the pattern.

If you don't want grep to do that, use the -q option. Then it will only set the return status, which is what if cares about.

(from man grep on a Linux system):

 -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...

Upvotes: 1

Related Questions