73

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?

  • Solved in bash 4.4+ – Isaac Nov 23 '18 at 1:18
84

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.

  • 8
    "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
  • 6
    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
  • 5
    Only single quotes worked for me. zsh was still interpreting \! and double quotes. – orkoden May 21 '14 at 16:29
  • 1
    On Solaris (rubbish old pre-XPG4 shell), '^' is an alias for | and is used to create a pipe. If you're sending scripts to customers and can't be sure what shell they'll run it in, you have to test with them all! – Nicholas Wilson Jan 16 '18 at 9:47
56

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.

  • 18
    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
  • 6
    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
  • @aculich, or just use the POSIX-specified command fc -14 instead. But it's true that you can do that without history expansion being enabled also. Personally, I use !$ and !vi and sudo !! and even git add !vi:$ often enough to warrant leaving history expansion enabled. – Wildcard Jan 16 '18 at 5:02
17

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 .

11

This also can do

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

Which works because bash concatenates adjacent strings. This approach is particularly useful when you have other things that need shell expansion, so you can't use single quotes for the entire string:

curl -v 'http://example.org/!'"287s87asdjh2/${basepath}/someresource"

! character is used for history expansions in command line prompt.
so this can be a problem in prompt but not in shell script files.
as you can see history expansions work even in double quotes.

  • 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
  • 2
    @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 '16 at 12:30
  • @SahilSingh How is this new? It concatenates three strings, two enclosed in double quotes and one enclosed in single quotes. There is no nesting here. – Raphael Mar 10 '17 at 6:44
  • @G-Man It is not obvious that when you put 2 strings next to each other they get concatenated. printf("hello""world") would work in c as well, but printf("hello"'w') won't work, so you see knowing that bash accommodates such expressions was new to me, but I do agree from utility point of view, this is not superior. I liked the answer, so did Mark Shust. – Sahil Singh Mar 10 '17 at 7:06
  • 2
    @G-Man It's also useful when there are other string expansions one does want to happen in the same string. This is an easy way of separating two types of quoting behavior. – WAF May 1 '17 at 15:09
6

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

E=!  
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.

2

Ever since Bash 4.3, you can now use double quotes to quote the history expansion character:

$ bash --version
GNU bash, version 4.3...
[...]
$ echo "Hello World!"
Hello World!
  • this doesnt work outside of echo, echo seems to handle this differently on its own – Blauhirn Sep 30 '17 at 12:04
  • @Blauhirn This has nothing to do with echo, and everything to do with quoting and the version of bash that you're running. – Flimm Oct 1 '17 at 13:38
  • 1
    This answer is wrong and should be deleted. Your bash version has nothing to do with the bang not being expanded, it's due to the fact that in your example the ! is followed by "end of line" and that prevents the shell from trying to expand it. Try echo "!Hello World" and you will see that bash will reply with bash: !Hello: event not found. See manual for more details – don_crissti Dec 19 '17 at 16:47
0

To those who are using git bash in windows, the accepted answer from @DanielPittman works. However, you should replace the backslash (\) with a forward slash (/).

For example, in unix, it'd look something like this:

curl https://abc.com/services -H 'Authorization: Bearer 111A80BBZZCnS\!ZR412543s'

For windows, it'd be something like this (focus on the forward slash in the authorization header part)

curl https://abc.com/services -H 'Authorization: Bearer 111A80BBZZCnS/!ZR412543s'

  • This doesn't make much sense. You have the argument single quoted, so regardless of the slashes involved the exclam won't result in history expansion. – Wildcard Jan 16 '18 at 5:03
  • Ohh you're right. I only posted this answer because when I was using Daniel's answer (using backslash), an error pops up. – SamuelDev Jan 16 '18 at 5:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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