This question already has an answer here:
Closed 3 years ago.
Let's say I have a for loop in bash which looks like this:
for i in $MYDIR/*.jar
do
# do something with files
done
Is the order of iteration guaranteed, i.e. will the loop always process files in same order? If it is guaranteed, is the order alphabetical?
According to the bash man page:
Pathname Expansion
After word splitting, unless the -f option has been set, bash scans
each word for the characters *, ?, and [. If one of these characters
appears, then the word is regarded as a pattern, and replaced with an
alphabetically sorted list of filenames matching the pattern (see
Pattern Matching below).
So, the files will be processed in alphabetical order (which depends on your locale, see comments) due to globbing expansion rules.