Why does this Bash regex match return an Exit Status of "2"?

Posted by PreservedMoose on Stack Overflow See other posts from Stack Overflow or by PreservedMoose
Published on 2014-05-27T03:21:08Z Indexed on 2014/05/27 3:26 UTC
Read the original article Hit count: 110

Filed under:
|
|

I'm writing a Bash script that needs to scan for the existence of non-ASCII characters in filenames. I'm using the POSIX bracket regex syntax to match the non-ASCII characters, but for some reason, when I test for the match in an if/then statement, the test always returns an Exit Status of 2, and never matches my test string.

Here's the code in question:

            FILEREQ_SOURCEFILE="Filename–WithNonAScII-Charàcters-05sec_23.98.mov"
            REGEX_MATCH_NONASCII="[^[:ascii:]]"

            if [[ $FILEREQ_SOURCEFILE =~ $REGEX_MATCH_NONASCII ]]; then

                    echo "Exit Status: $?"
                    echo "Matched!"

                else
                    echo "Exit Status: $?"
                    echo "No Match"
            fi

This code always returns:

Exit Status: 2
No Match

I've read and re-read the bash-hackers.org explanation of how regex matching works, as well as this previous question on SO regarding matching non-ASCII characters, but for the life of me, I can't get this to work. What am I missing here?

© Stack Overflow or respective owner

Related posts about regex

Related posts about bash