First, you don’t need ls
. By using ls
in backtics, you implicitly make bash parse a string into a list, which splits by whitespaces. Instead, make bash generate the list and separate it without such quirks:
Also, you need to enclose all $i
usages into quotes, to make bash substitute it as a whole, not as a string split to separate words.
Here’s the script that demonstrates both ideas:
for i in *.jpg ; do
echo "$i";
done