surajz
surajz

Reputation: 3611

bash escape ~ tilde

Does tilde character need to be escaped in bash script?

I have tried to escape it with \~ but it does not help. If I remove the ~ character, the code below works as expected.

            if ! [[ "$line" =~ ^[0-9a-zA-Z-~]+$ ]]; then
                    echo "skipping .. $line"
                    continue
            fi

How do I add the tilde character in the above expression?

Upvotes: 4

Views: 1409

Answers (1)

user80168
user80168

Reputation:

Don't put ~ after -. Change the regexp to:

if ! [[ "$line" =~ ^[0-9a-zA-Z~-]+$ ]]; then

and you'll be fine.

Take a look to this post for more explanations why hyphen may be the last element of the class.

Upvotes: 8

Related Questions