bash exclamation (!)

在bash處理有帶!的字串要注意他本身有其他功用(history expansion),作為字串夾在double quote時

https://www.gnu.org/software/bash/manual/bashref.html#Double-Quotes

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

要注意的是escape !不可用 \ backslash
\! 會是 \! 而不是!

The backslash preceding the ‘!’ is not removed.

用 single quote ‘!’ 可以解決此問題
echo “123”‘!’

3.1.2.2 Single Quotes
Enclosing characters in single quotes (‘'’) preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

This entry was posted in System Administration. Bookmark the permalink.

Leave a Reply