微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么我不能回应人物#!用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?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐