Is the order of iteration in bash for loop guaranteed? [duplicate]

天涯浪子 提交于 2019-12-18 06:56:57

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!