Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 35 down vote accepted

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").

share|improve this answer
"http://example.org/\!132" actually expands without interpreting the backslash (POSIX compliance reasons, I believe). – Chris Down Mar 3 '12 at 19:14
@ChrisDown, I tried to clarify that was my second option in the text. Thanks for pointing out the potential for confusion. – Daniel Pittman Mar 3 '12 at 22:30
For the record: It's not portable to try escaping "!". The best-practices recommendation is to always quote (singe-quotes) "!". Related: "^" (caret), is a non-metacharacter that needs quoting for portability. Finally, "!" should not be used in an if statement; use it as an argument to test instead if possible (again because of Solaris /bin/sh). – Nicholas Wilson Oct 18 '12 at 10:31
Only single quotes worked for me. zsh was still interpreting \! and double quotes. – orkoden May 21 '14 at 16:29

As well as the answer given by Daniel, you can also simply turn off history expansion altogether if you don't use it with set +H.

share|improve this answer
Turning off history expansion altogether is the best advice I've heard all day! History expansion is dangerous and byzantine when there are much better alternatives (incremental history search with Ctrl-R) that let you preview & edit your command so you don't blindly fire away with command !-14 that you though was at !-12 that, oops, happened to be rm -rf *. Be safe. Disable history expansion! Eschew the !! – aculich Mar 6 '12 at 1:09
Biggest answer: history expansion is a huge security risk! It can be used to attack your Unix through a crafted URL. – daniel Azuelos Jun 15 '15 at 7:30

I would personally do single quotes, but for completeness, I will also note since it is a URL, you can encode the ! as %21, e.g. curl -v http://example.org/%21132 .

share|improve this answer

This also can do

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

share|improve this answer
There are lots of ways of making Unix commands and English sentences use more characters than they need to, and be more confusing than they need to be.  How is this superior to the first / accepted / highest-voted answer, namely, putting the entire URL into single quotes? – G-Man Jun 15 '15 at 7:10
Thank you -- I liked this solution a lot. – Mark Shust Apr 1 at 13:47
@G-Man : It tells another way to construct bash arguments. I wasn't aware of this method. Nothing wrong in learning new stuff. – Sahil Singh Jul 6 at 12:30

I have come across the same problem, and my simple solution was to use a variable:

curl -v "http://example.org/${E}287s87asdjh2/somepath/someresource"

Here the simplicity is that (1) It is portable across shells and commands (2) Does not require knowing escape syntax and ASCII codes.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.