In my directory I have two files with space, foo bar and another file. I also have two files without space, file1 and file2.
The following script works:
for f in foo\ bar another\ file; do file "$f"; done
This script also works:
for f in 'foo bar' 'another file'; do file "$f"; done
But the following script doesn't work:
files="foo\ bar another\ file"
for f in $files; do file "$f"; done
Not even this script works:
files="'foo bar' 'another file'"
for f in $files; do file "$f"; done
But, if the files do not contain space, the script works:
files="file1 file2"
for f in $files; do file "$f"; done
Thanks!
Edit
Code snippet of my script:
while getopts "i:"a:c:d:f:g:h" arg; do
case $arg in
i) files=$OPTARG;;
# ...
esac
done
for f in $files; do file "$f"; done
With files without spaces, my script works. But I would like to run the script passing files with spaces as argument in one of these ways:
./script.sh -i "foo\ bar another\ file"
./script.sh -i foo\ bar another\ file
./script.sh -i "'foo bar' 'another file'"
./script.sh -i 'foo bar' 'another file'