Search code examples
bashshellglob

Iterate Over Files in Variable Path (Bash)


I was looking for the best way to find iterate over files in a variables path and came across this question.

However, this and every other solution I've found uses a literal path rather than a variable, and I believe this is my problem.

for file in "${path}/*"
do
     echo "INFO - Checking $file"
     [[ -e "$file" ]] || continue
done

Even though there are definitely files in the directory (and if i put one of the literal paths in place of ${path} I get the expected result), this always only iterates once, and the value of $file is always the literal value of ${path}/* without any globbing.

What am I doing wrong?


Solution

  • Glob expansion doesn't happen inside quotes (both single and double) in shell.

    You should be using this code:

    for file in "$path"/*; do
         echo "INFO - Checking $file"
         [[ -e $file ]] || continue
    done