I have not been able to find a solution to this seemingly simple problem here or anywhere else. I want to get the paths of all files in a nested directory, add quotes around them, and loop through them in a bash script. Directory names and files can have white spaces in them. So far nothing I tried works properly. The quoted path strings always get broken up at the spaces.
test.sh
for var in "$@"
do
echo "$var"
done
I tried reading from a file with one path per line, both single and double quotes:
find "nested directory with spaces" -type f | sed -e 's/^/"/g' -e 's/$/"/g' | tr '\n' '\n' > list.txt # double quotes
./test.sh `cat list.txt`
find "nested directory with spaces" -type f | sed -e 's/^/'\''/g' -e 's/$/'\''/g' | tr '\n' ' ' > list.txt # single quotes
./test.sh `cat list.txt`
and command substitution with a space between quoted paths, single and double quotes:
./test.sh `find "nested directory with spaces" -type f | sed -e 's/^/"/g' -e 's/$/"/g' | tr '\n' ' '` # double quotes
./test.sh `find "nested directory with spaces" -type f | sed -e 's/^/'\''/g' -e 's/$/'\''/g' | tr '\n' ' '` # single quotes
Simply echoing a quoted path from the command line gives the desired result. What is missing in the script that can resolve the arguments into complete strings?