4

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?

surajz
  • 3,471
  • 3
  • 32
  • 38

1 Answers1

8

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.

Community
  • 1
  • 1