为什么bash的"回声[T]"导致" T"不" [T]" [英] Why does bash "echo [t]" result in "t" not "[t]"
问题描述
此情况用于字符t和的值根。令人困惑
$回声[S]
[S]
$回声[T]
ŧ
$回声[T]
[T]
$回声[根]
ŧ
作为一个没有外壳常客(并不愿意成为),我发现令人惊讶的扩展名是如何设计的时候没有找到匹配的行为。我会报告猛砸参考
猛砸扫描每个字的字符
*
,?
和[
。如果一个
这些字符出现,则该字被视为图案,并
匹配文件名的字母顺序排序列表取代
模式。如果没有找到匹配的文件名:
- 如果shell选项
nullglob
被禁用,这个词是不变
- 如果shell选项
nullglob
置一词被删除
- 如果在
failglob
外壳选项设置,错误信息被打印并且不执行命令
块引用>好消息是,这个东西是可配置的。坏的是脚本就可以在许多方面一个不希望失败 - 至少,我没有,我花了一些时间来了解为什么
回声
行为您发布的方式,只是为了找到那是因为怪异的文件名的组合(谁曾经想命名文件T
?),隐藏的配置(nullglob
禁用,默认选项,但仍隐藏)和无害的命令。我说的无害的,因为这是你得到的,例如,当目标为
LS
(失败了,因为找不到文件):拉菲尔@毕宿五:〜$的mkdir测试
拉菲尔@毕宿五:〜$ CD测试
拉菲尔@毕宿五:〜/测试$按t
拉菲尔@毕宿五:〜/测试$ LS [T]
ŧ
拉菲尔@毕宿五:〜/测试$ LS [V]
LS:无法访问[V]:没有这样的文件或目录This happens for the character t, and the value root. Quite perplexing
$ echo [s] [s] $ echo [t] t $ echo [ t ] [ t ] $ echo [root] t
解决方案Being not a shell habitué (and not willing to become) I found surprising how filename expansion is designed to behave when no matches are found. I'll report the Bash reference
Bash scans each word for the characters
*
,?
, and[
. If one of these characters appears, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of file names matching the pattern. If no matching file names are found:
- if the shell option
nullglob
is disabled, the word is left unchanged- if the shell option
nullglob
is set the word is removed- If the
failglob
shell option is set, an error message is printed and the command is not executedThe good news is this thing is configurable. The bad one is a script can fail in a number of ways one doesn't expect - at least, I did not, and it took me some time to understand why
echo
behaves the way you posted, just to find that it's because of a combination of weird filenames (who ever wants to name a filet
?), hidden configuration (nullglob
disabled, default option but still hidden) and a harmless command.I said harmless because this is what you get, for example, when the target is
ls
(a failure because the file is not found):raffaele@Aldebaran:~$ mkdir test raffaele@Aldebaran:~$ cd test raffaele@Aldebaran:~/test$ touch t raffaele@Aldebaran:~/test$ ls [t] t raffaele@Aldebaran:~/test$ ls [v] ls: cannot access [v]: No such file or directory
这篇关于为什么bash的"回声[T]"导致" T"不" [T]"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!