Within a bash script, I'm trying to pull all files with an extension '.jstd' into an array, loop over that array and carry out some action.
My script is failing to copy the path of each script into the array.
I have the following script.
#!/bin/bash
IFS=$'\n'
file_list=($(find '/var/www' -type f -name "*.jstd"))
for i in "${file_list[@]}"; do
echo "$i"
done
echo $file_list
unset IFS
The line file_list=($(find '/var/www' -type f -name "*.jstd")) works fine in the terminal, but fails in the script with:
Syntax error: "(" unexpected
I've googled, but failed. All ideas gratefully received.
edit: In case it helps in reproduction or clues, I'm running Ubuntu 12.04, with GNU bash, version 4.2.25(1)-release (i686-pc-linux-gnu)
mapfilemight be useful here for bash 4+.sh yourscript? If so, the manual use ofshwill override the#!/bin/bashshebang. (Also, allow me to strongly echo the advice given by @EtanReisner; reading fromfindinto an array in this manner is a very lossy process).echo $file_listdoesn't make sense for an array.printf '%q\n' "${file_list[@]}"would make sense, sure, but as it is your code only prints the first array entry, and that after string-splitting and glob-expanding it (since you don't quote correctly).