Reputation: 315
Why does not work first and second variants, but third works
#!/bin/sh
#---------------------------------------------
text="my:?text=this:one"
if (echo $text | grep '^my\:\?text=this\:one') then
echo "1"
elif (echo $text | grep '^my:\?text=this:one') then
echo "2"
elif (echo $text | grep 'text=this:one') then
echo "3"
fi
Upvotes: 1
Views: 134
Reputation: 10757
Use Egrep for Extended grep functionality :
echo $text | egrep '^my\:\?text=this\:one'
Upvotes: 3
Reputation: 11946
grep does not use regular expressions by default, add the -E
flag to enable extended regular expressions.
Edit: grep does not use extended regular expressions by default, and grep -E
is usually aliased to egrep
for quicker use
Upvotes: 7
Reputation: 242198
Remove the backslash before the question mark. It is not considered a special character by grep
. On the contrary, adding the backslash adds the special meaning to it.
Upvotes: 1
Reputation: 3744
Because :
is not a special symbol in regex and doesn't need escaping.
Upvotes: 1