Skip to main content
Wrong shell refered to in title (presumably)
Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k

Regular Expression Bracket Expression with dashbash

Source Link

Regular Expression Bracket Expression with dash

I want to write a regular expression that only allows a user to enter an input which contains alphanumeric characters, underscores, periods, dashes, and plus signs.

In a bash script, I have:

VALID="^[0-9a-zA-Z_\.\-\+]+$"
THE_INPUT=$1
if [[ ! $THE_INPUT =~ $VALID ]]; then
    echo "ERROR"
    exit 1  
fi

Why does this not work when the user enters an input which has a - in it?

Example user inputs:

  1. great-food.png

    i. produces error

  2. great_+food.png

    i. doesn't produce error