glob在Bash中与[[]] vs []有什么不同?

问题描述:

我有一个文件$ HOME /富/ foo.movglob在Bash中与[[]] vs []有什么不同?

if [[ -f $HOME/**/*.mov ]]; then 
    echo "file is there" else 
    echo "file is not there" 
fi 

回声 “文件不存在”。然而,

if [ -f $HOME/**/*.mov ]]; then 
    echo "file is there" else 
    echo "file is not there" 
fi 

回声 “文件中有”。

为什么[[]]和[]之间的区别?

+1

相关:https://unix.stackexchange.com/questions/306111/confused-about-operators-vs-vs-vs –

+0

shellcheck告诉我[-f]不能与globs一起使用。真的吗? – Bleakley

+1

@Bleakley我怀疑是这样的,glob在执行命令前展开了,所以如果有多个事情与glob相匹配,那么'''f'会有太多参数,如果'nullglob'启用了'如果glob不匹配任何东西,那么d太少了。你想知道是否有任何文件,就是这样吗? –

[命令的参数受扩展路径名的限制;在[[ ... ]]中使用的词语不是。

如果你得到file is there,我怀疑你完全有一个文件匹配的模式。否则,对于-f,您将有太多参数,或者该模式将被视为不命名现有文件的文字字符串。