Bash Regex – How to Match Both Upper and Lower Case Letters

bashregular expression

we set the following variables

status=ok
echo $status
ok

now we want to verify if variables with regex will match

as the following

[[ $status =~ [OK]  ]] && echo "is the same"
[[ $status =~ OK  ]] && echo "is the same"
[[ $status =~ "OK"  ]] && echo "is the same"

but any of the above not print "is the same"

what is wrong in my regex?

Best Answer

[OK] Will match either character within the brackets, the brackets do not tell it to be case insensitive.

You could do:

[[ "$status" =~ ^[Oo][Kk]$ ]]

or I would probably do the following:

[[ "${status,,}" == ok ]]

The ,, operator for parameter expansion will convert the entire variable to lowercase for the purpose of the comparison.

Related Question