shopt的nullglob意外地影响读取



我希望两个输出都等于foo [Required] bar,但在设置nullglob时,[Required]消失。为什么?

我正在运行MacOS。

谢谢!

$ bash --version
GNU bash, version 5.0.18(1)-release (x86_64-apple-darwin19.5.0)
$ shopt -u nullglob
$ read < <(echo "foo [Required] bar"); echo $REPLY;
foo [Required] bar
$ shopt -s nullglob
$ read < <(echo "foo [Required] bar"); echo $REPLY;
foo bar

由于您没有引用变量$REPLY,文件名扩展是在扩展变量之后完成的。

[Required]是文件名通配符模式。它将匹配以下任何字符:Requird

您可能没有这样的文件名。nullglob选项意味着与任何内容都不匹配的通配符应该替换为空字符串,而不是保持未展开状态。

这个故事的寓意是:引用你的变量,除非你想让它们进行文件名扩展。

最新更新