I'm trying to loop over images in a directory, but instead of one image per loop, my for-loop only iterates once and the loop variable contains all the images.
#!/bin/bash
IMAGEDIR="/home/user/myimages/"
for file in "$IMAGEDIR*.jpg"; do
echo $file
echo "next file"
done
Outputs:
/home/user/myimages/A.jpg /home/user/myimages/B.jpg /home/user/myimages/C.jpg
next file
I would expect it to output:
/home/user/myimages/A.jpg
next file
/home/user/myimages/B.jpg
next file
/home/user/myimages/C.jpg
next file
I've looked at several other questions, and I don't think I should need a nested for loop.
- How to loop over directories in Linux?
- Looping over directories in Bash
- Loop through files in a given directory
- loop over files and extract part of filename
Additionally, I figured out that it works if I use a slightly different format
#!/bin/bash
IMAGEDIR="/home/user/myimages/"
for file in "$IMAGEDIR"*.jpg; do
echo $file
echo "next file"
done
Why does the first glob return a single string and the second one return a list?