为什么我不能回应人物#!用bash中的字符串?

参见英文答案 > echo “#!” fails — “event not found”5个
我有一个小的bash脚本应该移动到我的主目录,创建一个文件,回收一些垃圾,并使其可执行.这就是它的样子:
cd ; touch tor.sh; echo "#!/bin/bash\n/usr/local/bin/tor" >> tor.sh; chmod +x tor.sh

但这仍然在回声中打破,抱怨“未找到事件”?出于某种原因,我决定尝试这个并且它有效:

cd ; touch tor.sh; echo -e "\x23\x21/bin/bash\n/usr/local/bin/tor" >> tor.sh; chmod +x tor.sh

为什么我必须用十六进制和-e替换这两个字符(shebang?)?有一个更好的方法吗?

这不是一个错误,它与shebang无关,只是感叹号.
Enclosing  characters  in double quotes preserves the literal value of
all characters within the quotes,with the exception of $,`,\,and,when  history expansion is enabled,!.

所以要么逃避它,使用单引号,要么关闭历史扩展.

例如

> echo "How dare you put an '!' in this string?"
bash: !: event not found
> set +o histexpand
> echo "How dare you put an '!' in this string?"
How dare you put an '!' in this string?

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...