简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM