Question: I have a file, I want to determine if it contains only English ASCII characters.

I thought this is a very common question, but when I googled it, there is no direct answer or related. 

According to some other questions, this is what I got so far, but this doesn't work. No matter ASCII or Non-ASCII chars, it will always determine as non-ascii.

```
if (LC_ALL=C; [[ $TEXT = *[[:ascii:]]* ]]) then
  echo "Contain Non-ASCII"
fi

```

BTW, This is a shell script.

Answer: 
Your code tests whether the value of the variable `TEXT` contains at least one ASCII character. If you want to test whether `$TEXT` contains non-ASCII characters, you need to invert the set. By the way, `[:ascii:]` is locale-independent, so you don't need to set `LC_CTYPE`.

```
if [[ $TEXT = *[![:ascii:]]* ]]; then
  echo "Contain Non-ASCII"
fi

```

Note that `[:ascii:]` is a bash (and zsh) feature. If you want your script to work in ksh or plain sh, you will need to make use of the locale. Here's a portable method:

```
LC_CTYPE=C
case $TEXT in
  *[![:cntrl:][:print:]]*) echo "Contain Non-ASCII";;
esac

```

If you want to check the file contents, use [grep](http://linux.die.net/man/1/grep).

```
if LC_ALL=C grep -q '[^[:cntrl:][:print:]]' somefile; then
  echo "Contain Non-ASCII"
fi

```
```
TEXT=SOMETEXTHERE

RESULT=$(echo $TEXT | LC_COLLATE=C grep -r '[^ -~]')
if [ -z "$RESULT" ]; then
    echo "ALL ASCII"
else
    echo "Contain Non-ASCII"
fi

```