0

I would like to run a shell script on files in multiple folders. At the moment, I have the following:

#/bin/bash

FILES=("./binBuild/*.c" "./binBuild/subdir1/*.c" "./binBuild/subdir2/*.c" "./binBuild/subdir2/subsubdir1/*.c")

for file in $FILES

do
    echo $file
done

However, this only lists files in "./binBuild/*.c". How can I correctly incorporate the other folders listed, into $FILES?

218
  • 1,754
  • 7
  • 27
  • 38
  • 4
    `for file in "${FILES[@]}"` – anubhava Feb 16 '18 at 17:58
  • Could you explain what that means/why your suggestion will work? – 218 Feb 16 '18 at 18:00
  • I had already posted an answer in dup question :) – anubhava Feb 16 '18 at 18:03
  • 1
    If you don’t quote the globs, they’ll be expanded right in the array, so you can quote the values correctly when fetching them. – Davis Herring Feb 16 '18 at 18:05
  • Unless there is a reason you want to refer back to the list of wildcards within the script, the trivial solution is `for files in ./binBuild/*.c ./binBuild/subdir1/*.c ./binBuild/subdir2/*.c ./binBuild/subdir2/subsubdir1/*.c; do`... (notice also the lowercase variable name). – tripleee Feb 19 '18 at 05:12

0 Answers0