bash for loop中的迭代顺序是否得到保证? [英] Is the order of iteration in bash for loop guaranteed?

查看:73
本文介绍了bash for loop中的迭代顺序是否得到保证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在bash中有一个for循环,如下所示:

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?

推荐答案

根据 bash手册页:

扩展路径名

Pathname Expansion

分词后,除非已设置-f选项,否则bash会扫描 * 、?和[.的每个单词.如果这些字符之一 出现,则将该单词视为一个模式,并替换为 与模式匹配的文件名按字母顺序排序(请参阅 模式匹配如下).

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.

这篇关于bash for loop中的迭代顺序是否得到保证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆