如果 glob 模式不匹配任何文件,bash
将只返回文字模式:
bash-4.1# echo nonexistent-file-*
nonexistent-file-*
bash-4.1#
您可以通过设置 nullglob
shell 选项来修改默认行为,这样如果没有匹配项,您将得到一个空字符串:
bash-4.1# shopt -s nullglob
bash-4.1# echo nonexistent-file-*
bash-4.1#
那么 ash
中是否有等效的选项?
bash-4.1# ash
~ # echo nonexistent-file-*
nonexistent-file-*
~ # shopt -s nullglob
ash: shopt: not found
~ #
最佳答案
对于没有 nullglob
的 shell,例如 ash 和 dash:
IFS="`printf '\n\t'`" # Remove 'space', so filenames with spaces work well.
# Correct glob use: always use "for" loop, prefix glob, check for existence:
for file in ./* ; do # Use "./*", NEVER bare "*"
if [ -e "$file" ] ; then # Make sure it isn't an empty match
COMMAND ... "$file" ...
fi
done
来源:Filenames and Pathnames in Shell: How to do it correctly ( cached )
关于bash - ash 是否具有与 bash 的 'nullglob' 选项等效的选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4839214/