bash for 循环中的迭代顺序是否有保证?

Is the order of iteration in bash for loop guaranteed?

假设我在 bash 中有一个 for 循环,它看起来像这样:

for i in $MYDIR/*.jar
do
    # do something with files
done

是否保证迭代顺序,即循环是否始终以相同顺序处理文件?如果有保证,顺序是按字母顺序吗?

根据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).

因此,由于 globbing 扩展规则,文件将按 字母顺序 处理(这取决于您的语言环境,请参阅评论)。