Bash – Why doesn’t this loop process one file at a time

bashshell-scriptwildcards

I've got a directory with various files:

main.js
SomeClass.js
View.jsx

And I want to loop through all the .jsx files. So I wrote this Bash script:

for JSX_FILE in "$BUILD_DIR/*.jsx"; do   
    echo $JSX_FILE
    echo "PATH: $JSX_FILE"
    JSX_FILENAME=$(basename "$JSX_FILE")
    echo "NAME: $JSX_FILENAME"
done

But for some reason that would print this:

/path/to/View.jsx

PATH: /path/to/*.jsx

NAME: *.jsx

So I don't understand why in one case $JSX_FILE has the value /path/to/View.jsx and in another case it has the value /path/to/*.jsx. How can I make sure this variable will have the same value everywhere within the loop?

Best Answer

Quoting the glob inhibits globbing.

for JSX_FILE in "$BUILD_DIR"/*.jsx; do   
Related Question