看到一些让我怀疑自己理智的事情:
#!/bin/env bash
set -e
for d in '/tmp/somedir/*'; do
for f in "${d}/*pub"; do
echo $f;
done
done
它按预期返回
/tmp/somedir/AAA/fileaa.pub /tmp/somedir/BBB/filebb.pub
但是,如果我改为echo $f
或echo "$f"
我echo "${f}"
得到:
/tmp/somedir/*/*pub
我很难理解为什么。首先,这是一个单独的项目,所以它不只是影响生产echo
线的东西。
使用GNU bash, version 5.2.37(1)-release (x86_64-pc-linux-gnu)
这是该主机上的商店
assoc_expand_once off
cdable_vars off
cdspell off
checkhash off
checkjobs off
checkwinsize on
cmdhist on
compat31 off
compat32 off
compat40 off
compat41 off
compat42 off
compat43 off
compat44 off
complete_fullquote on
direxpand off
dirspell off
dotglob off
execfail off
expand_aliases on
extdebug off
extglob on
extquote on
failglob off
force_fignore on
globasciiranges on
globskipdots on
globstar off
gnu_errfmt off
histappend on
histreedit off
histverify off
hostcomplete off
huponexit off
inherit_errexit off
interactive_comments on
lastpipe off
lithist off
localvar_inherit off
localvar_unset off
login_shell off
mailwarn off
no_empty_cmd_completion off
nocaseglob off
nocasematch off
noexpand_translation off
nullglob off
patsub_replacement on
progcomp on
progcomp_alias off
promptvars on
restricted_shell off
shift_verbose off
sourcepath on
varredir_close off
xpg_echo off
修正脚本后:
通常,我们不应该在循环中引用
*
或?
。这样,Bash 将扩展它们以匹配每个文件名,并且 for 循环将按预期运行。