There are several things wrong with this script:
FILES is not an array.
- Never use the output of
ls programmatically.
FILE and File are two different variables; names are case-sensitive.
- All-uppercase names are reserved for use by the shell.
Your code should be
files=( *txt ) # ( *.txt ) would probably be cleaner
for file in "${files[@]}"; do
echo "$file"
done
Be aware, though, that depending on your shell settings, the assignment to files can have three different outcomes if there are no matching files:
files could contain a single element, literally *txt (the default)
files could be empty (with nullglob set)
- The pattern match could produce an error (with
failglob set)