How to check if a variable contains a character not found in a list in a BASH conditional?

advertisements

I need to create a BASH conditional that checks if any unusual characters are found. If any character is not one of these, the conditional returns true: c, d, f, g, h, T, ,, ?, !. E.g.:

if [[ "$variable" contains something that is not [cdfghZ,?!] ]]
then
    echo "The variable contains an unknown character."
fi

How can I check for characters not found in a list with a BASH conditional?


You can use globbing:

[[ "$variable" == *[^cdfghT,?\!]* ]]

PS: ! needs to be used as \! to escape history event expansion.