为什么我不能呼应字符#!在bash中的字符串? [英] Why can't I echo the characters #! in a string in bash?

查看:98
本文介绍了为什么我不能呼应字符#!在bash中的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是应该移动到我的主目录,使一个文件一个很小的bash脚本,回声一些垃圾到它,并使其可执行文件。这是它看起来像:

I had a tiny bash script that is supposed to move to my home directory, make a file, echo some junk onto it, and make it executable. This is what it looked like:

cd ; touch tor.sh; echo "#!/bin/bash\n/usr/local/bin/tor" >> tor.sh; chmod +x tor.sh

但是,这打破保持在回声,抱怨事件未找到?出于某种原因,我决定试试这个,它的工作:

But this kept breaking at the echo, complaining about "event not found" ? For some reason I decided to try this and it worked:

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

为什么我要那两个字符(家当?)用十六进制和-e更换?有没有更好的方式来做到这一点?

Why did I have to replace those two characters (the shebang?) with hex and a -e? Is there a better way to do this?

推荐答案

这是不是一个错误,它没有任何关系的家当,仅仅是感叹号。

This is not a bug and it has nothing to do with the shebang, just the exclamation point.

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, !.

因此​​,要么逃避它,使用单引号,或者关闭历史扩展。

So either escape it, use single quotes, or turn off history expansion.

例如

> 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?

这篇关于为什么我不能呼应字符#!在bash中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆