如何在Linux bash shell中逃避bang(!)字符?

时间:2022-05-18 05:31:51

I cannot for the life of me figure out how to escape the ! character in Bash. For example, I want to print this:

我不能为我的生活弄清楚如何逃脱! Bash中的角色。例如,我想打印这个:

Text text text!

文字文字!

I've tried this:

我试过这个:

echo "Text text text\!"

but that prints this instead:

但是打印出来的是:

Text text text\!

文字文字\!

(with an extra backslash).

(带有额外的反斜杠)。

How can I do this?

我怎样才能做到这一点?

3 个解决方案

#1


10  

Try this:

尝试这个:

 echo 'Text text text!'

or

要么

 echo "Text text text"'!'

or

要么

 echo -e "Text text text\x21"

#2


4  

Single quote:

单引号:

echo 'Text Text Text!'

That does it for me.

这对我来说。

#3


0  

As other answers have indicated the answer to doing this in the general case is to use single quotes (as they do not evaluate history expansion).

正如其他答案所表明的那样,在一般情况下这样做的答案是使用单引号(因为它们不评估历史扩展)。

As Cyrus's answer indicates you can use double quotes for the main text and single quotes only for the exclamation point if that's what you need. Remember the shell doesn't care how the quoting works out it sees the "words" after the quotes come off.

正如Cyrus的回答所示,如果您需要,可以使用双引号作为主文本,单引号仅用于感叹号。请记住shell不关心引用是如何工作的,它会在引号脱落后看到“单词”。

That all being said in non-interactive contexts history expansion is off by default and so this escaping/etc. is not necessary.

这一切都在非交互式环境中被说明历史扩展默认是关闭的,所以这个逃避/等等。没有必要。

$ cat echo.sh
echo Text Text Text!
$ bash echo.sh
Text Text Text!

In theory you should be able to manually enable history expansion in non-interactive contexts and run into this problem but my quick tests at using set -o hsitory -o histexpand in that script before the echo command did not trigger history expansion issues.

从理论上讲,您应该能够在非交互式上下文中手动启用历史记录扩展并遇到此问题但我在echo命令之前使用set -o hsitory -o histexpand在echo命令之前没有触发历史记录扩展问题的快速测试。

#1


10  

Try this:

尝试这个:

 echo 'Text text text!'

or

要么

 echo "Text text text"'!'

or

要么

 echo -e "Text text text\x21"

#2


4  

Single quote:

单引号:

echo 'Text Text Text!'

That does it for me.

这对我来说。

#3


0  

As other answers have indicated the answer to doing this in the general case is to use single quotes (as they do not evaluate history expansion).

正如其他答案所表明的那样,在一般情况下这样做的答案是使用单引号(因为它们不评估历史扩展)。

As Cyrus's answer indicates you can use double quotes for the main text and single quotes only for the exclamation point if that's what you need. Remember the shell doesn't care how the quoting works out it sees the "words" after the quotes come off.

正如Cyrus的回答所示,如果您需要,可以使用双引号作为主文本,单引号仅用于感叹号。请记住shell不关心引用是如何工作的,它会在引号脱落后看到“单词”。

That all being said in non-interactive contexts history expansion is off by default and so this escaping/etc. is not necessary.

这一切都在非交互式环境中被说明历史扩展默认是关闭的,所以这个逃避/等等。没有必要。

$ cat echo.sh
echo Text Text Text!
$ bash echo.sh
Text Text Text!

In theory you should be able to manually enable history expansion in non-interactive contexts and run into this problem but my quick tests at using set -o hsitory -o histexpand in that script before the echo command did not trigger history expansion issues.

从理论上讲,您应该能够在非交互式上下文中手动启用历史记录扩展并遇到此问题但我在echo命令之前使用set -o hsitory -o histexpand在echo命令之前没有触发历史记录扩展问题的快速测试。