#!/bin/env bash
set -e
for d in '/tmp/somedir/*'; do # `*` and `?` should not be quoted unless they exist in the filenames
for f in "${d}/*pub"; do
echo $f; # Here, the variable `f` is actually `/tmp/somedir/*/*pub`
# Without double quotes, Bash will expand the glob at this point
# So, you will see all matching files.
# With double quotes, you will see the original value of var `f`
done
done
更正脚本后:
#!/bin/env bash
set -e
for d in /tmp/somedir/*; do
for f in "${d}"/*pub; do # only quote the variable
echo "$f"; # you will get each filename
done
done
通常,我们不应该引用
*
或
?
消息灵通的这样,Bash将展开它们以匹配每个文件名,for循环将按预期运行。