Handle special characters in bash for...in loop
bash, command-line, shell
Solution
UPDATE BY OP: this answer sucks and shouldn't be on top ... @Jordan's post below should be the accepted answer.
one possible way:
ls -1 | while read x; do
echo $x
done
Problem
Suppose I've got a list of files ``` file1 "file 1" file2 ``` a for...in loop breaks it up between whitespace, not newlines: ``` for x in $( ls ); do echo $x done ``` results: ``` file 1 file1 file2 ``` I want to execute a command on each file. "file" and "1" above are not actual files. How can I do that if the filenames contains things like spaces or commas? It's a little trickier than I think find -print0 | xargs -0 could handle, because I actually want the command to be something like "convert input/file1.jpg .... output/file1.jpg" so I need to permutate the filename in the process.