SoFunction
Updated on 2025-03-10

Summary of exclamation marks in double quotes in Bash Shell

Detailed explanation of exclamation marks in double quotes in Bash Shell

In Bash Shell, exclamation marks (!) is a special character that is mainly used for historical expansion. History extension allows you to use!To quote historical commands. However, when you use exclamation marks in double quotes, if you use exclamation marks directly in double quotes, it may be interpreted as part of the historical extension.

Exclamation mark problem in double quotes

For example:

echo "Hello!"

This command may cause an error in some cases because the Shell attempts to!Interpreted as a historical extension.

How to deal with it

To avoid this problem, there are several ways to deal with it:

Use single quotes

Using single quotes to surround strings prevents historical expansion, because single quotes do not perform any extensions:

echo 'Hello!'

Escape exclamation mark

Add a backslash before the exclamation mark\to escape it, thus preventing historical expansion:

echo "Hello\!"

Close the historical extension

You can runset +HThe command temporarily turns off the historical extension function:

set +H
echo "Hello!"
set -H  # If needed,Can restart the history extension

This is the end of this article about the detailed explanation of the exclamation mark problem in double quotes in Bash Shell. For more related content of Bash Shell double quotes exclamation marks, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!