Escaping asterisk * in Bash on Windows

1

The following command works in GNU Bash on FreeBSD but not in Git Bash on Windows:

curl -X PUT https://example.com/_config/cors/origins -d '"*"'

The intended result is to send a PUT request to https://example.com/_config/cors/origins with the body "*" (including the quotes - it's a JSON string).

However on Windows, the asterisk gets expanded as a glob even though it’s within quotes. Excerpt from --trace-ascii cURL log:

0000: PUT /_config/cors/origins HTTP/1.1
004f: User-Agent: curl/7.30.0
008e: Content-Length: 13
00a2: Content-Type: application/x-www-form-urlencoded
00d3: 
=> Send data, 13 bytes (0xd)
0000: .editorconfig
== Info: upload completely sent off: 13 out of 13 bytes

(.editorconfig is the first file in the current directory.)

Escaping with a backslash ('"\*"') transmits the backslash:

0000: PUT /_config/cors/origins HTTP/1.1
004f: User-Agent: curl/7.30.0
008e: Content-Length: 4
00a1: Content-Type: application/x-www-form-urlencoded
00d2: 
=> Send data, 4 bytes (0x4)
0000: "\*"
== Info: upload completely sent off: 4 out of 4 bytes

Two backslashes also transmits both backslashes in the request.

0000: PUT /_config/cors/origins HTTP/1.1
004f: User-Agent: curl/7.30.0
008e: Content-Length: 5
00a1: Content-Type: application/x-www-form-urlencoded
00d2: 
=> Send data, 5 bytes (0x5)
0000: "\\*"
== Info: upload completely sent off: 5 out of 5 bytes

Is this a bug?

Tamlyn

Posted 2015-04-03T21:05:02.640

Reputation: 280

I posted an answer but when you say, you used two backslashes can you actually edit your post to show us an example? Or perhaps just edit to show all the different ideas/attempts you made? That would be helpful from a debugging standpoint. – JakeGould – 2015-04-03T21:19:59.613

Are you sure bash is the culprit and not curl? What curl are you using (native build or cygwin/msys one)? Some versions of libc for Windows will expand * (but not other globs) while parsing the command line (from GetCommandLine to argc/argv) -> can you test with cmd.exe (that passes the command line unchanged) if you can also see the "globbing"? – mihi – 2015-04-04T10:41:10.023

When using a cmd prompt I can get it to work by passing -d \"*\" – Tamlyn – 2015-04-04T12:31:41.537

and when you try -d '\"*\"' in bash, it does not work? – mihi – 2015-04-04T12:50:17.117

nope, it sends the backslashes too. – Tamlyn – 2015-04-06T15:30:01.567

Answers

0

Try it like this idea based on escaping tips and ideas found on this site:

curl -X PUT https://example.com/_config/cors/origins -d "\*"

Another idea comes from this answer on a similar question about sending data via a POST request. First create a “data” file called “data.txt” that simply contains the *. Then run this curl command:

curl -X POST -d @data.txt https://example.com/_config/cors/origins

You could try that without the -X POST which sets the request method like this:

curl -d @data.txt https://example.com/_config/cors/origins

JakeGould

Posted 2015-04-03T21:05:02.640

Reputation: 38 217