J.B.
2017-06-25 19:25:32 UTC
Expanding an array for use in a for-loop behaves differently depending
on how the array was created. How can I get bash to expand using a
different delimiter? I tried `find -print0' after setting IFS= but bash
just ignores it (and says so). I then tried `find -exec ls
--quoting-style=shell-escape-always', but that didn't work either.
$ !find
find ./path/ -type f -name \*.txt
./path/filename with spaces.txt
./path/good_filename.txt
$ listoffiles=($(find ./path/ -type f -exec ls
--quoting-style=shell-escape-always '{}' \;))
$ echo ${listoffiles[@]}
'./path/filename with spaces.txt' './path/good_filename.txt'
$ for file in ${listoffiles[@]}; do echo $file; done
'./path/filename
with
spaces.txt'
'./path/good_filename.txt'
$ for file in "${listoffiles[@]}"; do echo $file; done
'./path/filename
with
spaces.txt'
'./path/good_filename.txt'
$ for file in "${listoffiles[*]}"; do echo $file; done
'./path/filename with spaces.txt' './path/good_filename.txt'
$ !unse
unset -v listoffiles
$ listoffiles=(
./path/filename
with
spaces.txt
./path/good_filename.txt
$ for file in "${listoffiles[@]}"; do echo $file; done
./path/filename with spaces.txt
./path/good_filename.txt
on how the array was created. How can I get bash to expand using a
different delimiter? I tried `find -print0' after setting IFS= but bash
just ignores it (and says so). I then tried `find -exec ls
--quoting-style=shell-escape-always', but that didn't work either.
$ !find
find ./path/ -type f -name \*.txt
./path/filename with spaces.txt
./path/good_filename.txt
$ listoffiles=($(find ./path/ -type f -exec ls
--quoting-style=shell-escape-always '{}' \;))
$ echo ${listoffiles[@]}
'./path/filename with spaces.txt' './path/good_filename.txt'
$ for file in ${listoffiles[@]}; do echo $file; done
'./path/filename
with
spaces.txt'
'./path/good_filename.txt'
$ for file in "${listoffiles[@]}"; do echo $file; done
'./path/filename
with
spaces.txt'
'./path/good_filename.txt'
$ for file in "${listoffiles[*]}"; do echo $file; done
'./path/filename with spaces.txt' './path/good_filename.txt'
$ !unse
unset -v listoffiles
$ listoffiles=(
'./path/filename with spaces.txt'
'./path/good_filename.txt'
)
$ for file in ${listoffiles[@]}; do echo $file; done'./path/good_filename.txt'
)
./path/filename
with
spaces.txt
./path/good_filename.txt
$ for file in "${listoffiles[@]}"; do echo $file; done
./path/filename with spaces.txt
./path/good_filename.txt