ste1inbeck
ste1inbeck

Reputation: 315

Bash quеstion (RegExp)

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

Answers (4)

Sandeep Pathak
Sandeep Pathak

Reputation: 10757

Use Egrep for Extended grep functionality :

echo $text | egrep '^my\:\?text=this\:one'

Upvotes: 3

J V
J V

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

choroba
choroba

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

Qnan
Qnan

Reputation: 3744

Because : is not a special symbol in regex and doesn't need escaping.

Upvotes: 1

Related Questions