编辑2:甚至做"ls*()"导致bash不返回并使用100%的一个cpu.有谁知道为什么bash这样做?
我正在学习C并学习参数,并且想知道当作为参数传递时,哪些角色做了奇怪的事情.我通过bash将没有引号的"*()"传递给ac程序,如:
$ ./program *()
Run Code Online (Sandbox Code Playgroud)
无法使用ctrl c或ctrl z退出Bash.当我看到htop时,它使用100%的一个CPU而且我必须使用SIGKILL.有谁知道这里发生了什么.我只是好奇.
编辑:即使是简单的程序
#include <stdio.h>
int main(int argc, char *argv[]){ return 0; }
Run Code Online (Sandbox Code Playgroud)
导致这种行为.
这似乎是bash中已知的错误,已在4.3.16版中修复.只有在extglob启用该功能时才会出现这种情况,例如,如果您有类似的命令
shopt -s extglob
Run Code Online (Sandbox Code Playgroud)
在您.bashrc或其他一些init文件中.
我可以在Linux Mint 17上使用bash 4.3.11一致地重现它:
$ bash --norc
bash-4.3$ mkdir empty
bash-4.3$ cd empty
bash-4.3$ echo *()
bash: syntax error near unexpected token `('
bash-4.3$ shopt -s extglob
bash-4.3$ echo *()
Run Code Online (Sandbox Code Playgroud)
shell在最后一个命令后挂起.请注意,我在一个空目录中运行它; 问题也发生在非空目录中.
bash手册记录了一种通配符形式,只有在extglob启用时才会启用:
`*(PATTERN-LIST)'
Matches zero or more occurrences of the given patterns.
Run Code Online (Sandbox Code Playgroud)
给定*(),这是空字符串的零次或多次出现.由于在任何字符串中出现无限多的空字符串,我可以看到,如果没有特殊情况代码可以避免它,那么这可能会导致无限循环.
它似乎已在以后的版本中修复.我在4.3.11中看到了问题,但在4.3.30中没有看到.rici的评论表明补丁016可能已经修复了它.与补丁对应的错误报告包括:
1) bash gets stuck
shopt -s extglob
echo !(*/) # never returns, cannot be interrupted
Run Code Online (Sandbox Code Playgroud)
我要说的确认那就是错误.