问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/37277666/is-the-order-of-iteration-in-bash-for-loop-guaranteed