Bash Special Characters – Can’t Use Exclamation Mark (!) in Bash

bashquotingspecial characters

I'm trying to use the curl command to access a http url with a exclamation mark (!) in its path. e.g:

curl -v "http://example.org/!287s87asdjh2/somepath/someresource"

the console replies with bash: ... event not found.

What is going on here? and what would be the proper syntax to escape the exclamation mark?

Best Answer

The exclamation mark is part of history expansion in bash. To use it you need it enclosed in single quotes (eg: 'http://example.org/!132') or to directly escape it with a backslash (\) before the character (eg: "http://example.org/\!132").

Note that in double quotes, a backslash before the exclam prevents history expansion, BUT the backslash is not removed in such a case. So it's better to use single quotes, so you're not passing a literal backslash to curl as part of the URL.

Related Question