We have a script running on our Debian server that grabs filenames in a directory and pushes them to our API. It runs fine when filenames don't have spaces. The usual answer to this common issue is to use double quotes around the variable name.
However, I can't find a tidy, brief and definitive solution for our particular case—code below. Although this answer has suggests changing the separator from space to \n, I'd rather get the double quote method right in our existing code.
Here's the relevant part of the code:
files=("$(ls $directory)") #$directory has the files we want to loop through if [ ${#files[@]} -gt 0 ]; then getToken for i in $files do echo "$i" uploadFiles "$i" done exit else echo "No files to upload" exit fi
ls.