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

前端之家收集整理的这篇文章主要介绍了为什么我不能回应人物#!用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?
原文链接:https://www.f2er.com/bash/386985.html

猜你在找的Bash相关文章