Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

3 Answers 3

up vote 21 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
1  
"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 at 16:29

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

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
3  
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

Your Answer

 
discard

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.