壳吃`?`字符

LSp*_*ice 1 shell bash special-characters quoting

这是一个如此简单的问题,我确定有人问过它,但我找不到。我的外壳,我没有故意设置这样做,似乎吃任何涉及问号的词:

$ bash --version
GNU bash, version 4.3.42(1)-release (x86_64-apple-darwin13.4.0)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

$ echo a ? = =?
a =

$ not-a-command? echo a
a

$ (?) echo a
a
Run Code Online (Sandbox Code Playgroud)

如果它很重要,请注意任何包含问号的单词似乎完全无声地消失了 - 在某种程度上,她的 shell 甚至从未注意到调用以未指定有效可执行文件的单词开头 - 即使问号是不是一开始。

Ste*_*itt 7

您可能nullglob启用了shell 选项;如果 glob 不匹配任何内容,这会导致删除包含通配符( *or ?) 的任何单词。因此,如果您所在的文件夹不包含任何具有单字符名称的文件、文件夹等,?则不会扩展为任何内容,而是将被删除;同样,not-a-command?不太可能匹配任何内容,而是将被删除。

要检查是否是这种情况,请运行

shopt nullglob
Run Code Online (Sandbox Code Playgroud)

要取消激活该选项,请运行

shopt -u nullglob
Run Code Online (Sandbox Code Playgroud)