Skip to main content
added 706 characters in body
Source Link
Stéphane Chazelas
  • 584.7k
  • 96
  • 1.1k
  • 1.7k

To correct an above answer (as I can't comment yet):

PHONE_TYPE="NORTEL"
if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO|SPACE TEL)$ ]]; then 
  echo "Phone type accepted."
else
  echo "Error! Phone type must be NORTEL, CISCO or NEC."
fi

Please note that you need at least bash 43.2 for this use of =~
It doesn't work in bash 3.=~

I tested on MS Windows 7 using bash 4.3.46 (works fine) and bash 3.1.17 (didn't work).

The LHS of the =~ should be=~ was introduced in quotes3. Above1 but in that version, PHONE_TYPE="SPACE TEL"you had to use:

[[ $PHONE_TYPE =~ '^(NORTEL|NEC|CISCO|SPACE TEL)$' ]]

In 3.2, the interface changed in that quoting operators such as '...' above would match toocause regexp operators (such as ^, (), |, $) to lose their special meaning¹, but space (which is otherwise special in the shell syntax if not the regexp syntax) could then be left unquoted if found within matching (...) pairs¹.

To do regexp matching portably across versions of bash 3.1+ and zsh, ksh93 and yash and avoid the bugs, best is to store the regexp in a variable:

phone_regexp='^(NORTEL|NEC|CISCO|SPACE TEL)$'
if [[ $PHONE_TYPE =~ $phone_regexp ]]; then...

¹ With some exceptions and bugs most of which have been fixed in the latest version.

To correct an above answer (as I can't comment yet):

PHONE_TYPE="NORTEL"
if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO|SPACE TEL)$ ]]; then 
  echo "Phone type accepted."
else
  echo "Error! Phone type must be NORTEL, CISCO or NEC."
fi

Please note that you need at least bash 4 for this use of =~
It doesn't work in bash 3.

I tested on MS Windows 7 using bash 4.3.46 (works fine) and bash 3.1.17 (didn't work)

The LHS of the =~ should be in quotes. Above, PHONE_TYPE="SPACE TEL" would match too.

To correct an above answer (as I can't comment yet):

PHONE_TYPE="NORTEL"
if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO|SPACE TEL)$ ]]; then 
  echo "Phone type accepted."
else
  echo "Error! Phone type must be NORTEL, CISCO or NEC."
fi

Please note that you need at least bash 3.2 for this use of =~

I tested on MS Windows 7 using bash 4.3.46 (works fine) and bash 3.1.17 (didn't work).

=~ was introduced in 3.1 but in that version, you had to use:

[[ $PHONE_TYPE =~ '^(NORTEL|NEC|CISCO|SPACE TEL)$' ]]

In 3.2, the interface changed in that quoting operators such as '...' above would cause regexp operators (such as ^, (), |, $) to lose their special meaning¹, but space (which is otherwise special in the shell syntax if not the regexp syntax) could then be left unquoted if found within matching (...) pairs¹.

To do regexp matching portably across versions of bash 3.1+ and zsh, ksh93 and yash and avoid the bugs, best is to store the regexp in a variable:

phone_regexp='^(NORTEL|NEC|CISCO|SPACE TEL)$'
if [[ $PHONE_TYPE =~ $phone_regexp ]]; then...

¹ With some exceptions and bugs most of which have been fixed in the latest version.

added 101 characters in body
Source Link
Will
  • 148
  • 5

To correct an above answer (as I can't comment yet):

PHONE_TYPE=NORTELPHONE_TYPE="NORTEL"
if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCONORTEL|NEC|CISCO|SPACE TEL)$ ]]; then 
  echo "Phone type accepted."
else
  echo "Error! Phone type must be NORTEL, CISCO or NEC."
fi

Please note that you need at least bash 4 for this use of =~
It doesn't work in bash 3.

I tested on MS Windows 7 using bash 4.3.46 (works fine) and bash 3.1.17 (didn't work)

The LHS of the =~ should be in quotes. Above, PHONE_TYPE="SPACE TEL" would match too.

To correct an above answer (as I can't comment yet):

PHONE_TYPE=NORTEL
if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then 
  echo "Phone type accepted."
else
  echo "Error! Phone type must be NORTEL, CISCO or NEC."
fi

Please note that you need at least bash 4 for this use of =~
It doesn't work in bash 3.

I tested on MS Windows 7 using bash 4.3.46 (works fine) and bash 3.1.17 (didn't work)

To correct an above answer (as I can't comment yet):

PHONE_TYPE="NORTEL"
if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO|SPACE TEL)$ ]]; then 
  echo "Phone type accepted."
else
  echo "Error! Phone type must be NORTEL, CISCO or NEC."
fi

Please note that you need at least bash 4 for this use of =~
It doesn't work in bash 3.

I tested on MS Windows 7 using bash 4.3.46 (works fine) and bash 3.1.17 (didn't work)

The LHS of the =~ should be in quotes. Above, PHONE_TYPE="SPACE TEL" would match too.

Source Link
Will
  • 148
  • 5

To correct an above answer (as I can't comment yet):

PHONE_TYPE=NORTEL
if [[ $PHONE_TYPE =~ ^(NORTEL|NEC|CISCO)$ ]]; then 
  echo "Phone type accepted."
else
  echo "Error! Phone type must be NORTEL, CISCO or NEC."
fi

Please note that you need at least bash 4 for this use of =~
It doesn't work in bash 3.

I tested on MS Windows 7 using bash 4.3.46 (works fine) and bash 3.1.17 (didn't work)