Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I escape an exclamation mark in bash?

Tags:

bash

Sometimes I feel the urge to put some more expressiveness in my git commit messages. Unfortunately bash does not seem to like this.

iblue@silence ~/git/wargames $ git commit -m "Frustrating <insert object of frustration here>!"
-bash: !": event not found

Escaping with a backslash helps, but this includes the backslash in the commit message.

How do I escape the exclamation mark in bash correctly?

like image 914
iblue Avatar asked Jun 14 '12 00:06

iblue


People also ask

How do I escape exclamation in bash?

'echo -e' and 'printf' interpret escaped octal, hex, and Unicode codes. In cases where you're using either of those, you can use "\041" (the octal for the "!") or "\x21" (the hex) to stand-in for the exclamation point.

What does exclamation mark do in bash?

Bash maintains the history of the commands executed in the current session. We can use the exclamation mark (!) to execute specific commands from the history.

How do I ignore special characters in bash?

Note the % <percent-sign> character within the printf argument. We can ignore its special meaning by escaping it with another <percent-sign>: %%. This preserves the literal value.

How do I escape a character in a shell script?

The escape (\) preceding a character tells the shell to interpret that character literally. With certain commands and utilities, such as echo and sed, escaping a character may have the opposite effect - it can toggle on a special meaning for that character.


2 Answers

Exclamation mark is preserved literally when you include it in a single-quoted string.

Example:

git commit -m 'Frustrating <insert object of frustration here>!'
like image 111
sblom Avatar answered Nov 15 '22 13:11

sblom


Have a try this one

git commit -m "Frustrating <insert object of frustration here>"'!'

If in the middle of string then

"hello"'!'"world"

like image 22
mug896 Avatar answered Nov 15 '22 13:11

mug896