toxic_boi_8041
toxic_boi_8041

Reputation: 1482

grep operation with " ? " pattern

I am going thru with different grep option. and I have following grep command

result=$(echo "ABC DEF" | grep -q " ? ")

I know, -q option in grep will silent the output.

Does " ? " have specific meaning in grep command or it will just match " ? " as string/characters?

Upvotes: 1

Views: 136

Answers (1)

Maroun
Maroun

Reputation: 95968

With no flags to indicate the input is a regex, it has no special meaning.

Test:

~$ echo "hello ? world" | grep " ? "
hello ? world

Test with the -q flag:

~$ echo "hello ? world" | grep -q " ? "; echo $?
0

$? holds the exit status of the last command. grep was the last command before echo, and it returns 0 when it matches.

If you try a non-matching string, you'll get:

~$ echo "hello world" | grep -q " ? "; echo $?
1

Upvotes: 3

Related Questions