Is the order of iteration in bash for loop guarant

2020-02-12 10:44发布

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?

标签: linux bash glob
1条回答
等我变得足够好
2楼-- · 2020-02-12 11:11

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.

查看更多
登录 后发表回答