bash:for file in if?

Chr*_*dal 4 bash loops file wildcard

所以,

for f in *.c; do echo "That $f is the best C code I have ever seen"; done
Run Code Online (Sandbox Code Playgroud)

如果没有c文件,很乐意打印

That *.c is the best C code I have ever seen
Run Code Online (Sandbox Code Playgroud)

这是不可取的.如果没有c文件,是否有一种优雅的方法来修复/表达我想完全跳过循环的事实?

Gil*_*il' 7

设置nullglob选项.如果没有匹配,则扩展将为空.

shopt -s nullglob
for f in *.c; do …
Run Code Online (Sandbox Code Playgroud)

请注意,这是一个特定于bash的构造,它不会在plain下工作sh.