简体   繁体   中英

What does the $'{}' mean in curl command

I am wondering what does the $ mean in the curl command when I am sending a POST.

eg curl -X POST mydomain.com -d $'{\\n"some.*.something": "myvalue"\\n}'

Also I am trying to parameterized the myvalue in my shell script, however I am unable to do it. Any suggestions?

The $'{\\n"some.*.something": "myvalue"\\n}' gets interpreted by the shell before it gets passed to curl.

$'...' are so called ANSI C strings . They exist in the shell language next to double-quoted strings "..." and single-quoted strings '...' .

Since the data itself - which seems to be json - contains double quotes, double quoted strings can't be used to wrap the data without escaping the double quotes within the data itself. Meaning it would need to look like this:

"{\n\"some.*.something\": \"myvalue\"\n}"

Basically single quoted strings could be used to avoid escaping the " .

'{\n"some.*.something": "myvalue"\n}'

But since the data contains the newline escape sequence \\n and escape sequences won't get expanded in single quoted strings , the author of the example in the question used ANSI C strings $'' .

Using ANSI C strings they get both: They can avoid to escape the double quotes in the data and still have the newline escape sequences getting expanded.

After the shell has expanded it, for curl the data looks like this:

{
"some.*.something": "myvalue"
}

Further read: https://www.gnu.org/software/bash/manual/html_node/Quoting.html

I still don't know what $'{}' mean. However, I did get myvalue to be parameterized.

"{\"some.*.something\":\"${myValueParamerterized}\"}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM