Bash need to test for alphanumeric string

the nominated answer is wrong. Because it doesn't check to the end of the string. also it's inverted. as the conditional says: "if the start of the string is valid characters then echo invalid"

[[ $TITLE =~ ^[a-zA-Z0-9_-]{3,20}$ ]] && ret="VALID" || ret="INVALID"

echo $ret

I'd invert the logic. Test for invalid characters and echo a warning if at least one is present:

if [[ "$TITLE" =~ [^a-zA-Z0-9] ]]; then
  echo "INVALID"
fi

With that said, your original check worked for me, so you probably need to provide more context (i.e. a larger portion of your script).


why cant we use alnum

[[ 'mystring123' =~ [:alnum:] ]] && echo "ok" || echo "no"