0

This is very similar to Prevent bash from adding single quotes to variable output, but none of those answers are helping me.

This script:

set -ex

curl_headers=(
    --silent
    "--form release_description=$'\n new \n'"
)

curl "${curl_headers[@]}" "example.com"

Returns this output:

+ curl_headers=(--silent "--form release_description=$'\n new \n'")
+ curl --silent '--form release_description=$'\''\n new \n'\''' example.com

It's doing two things I don't want. It's adding a single tick before --form and it's escaping the single ticks in my release_description

The desired result is:

curl --silent --form release_description=$'\n new \n' example.com

How do I get it to remove the escape sequences and added single ticks?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 1
    Please take a look at the last paragraph here: https://stackoverflow.com/a/25777854/1566221 – rici Sep 26 '18 at 21:35
  • 2
    `$''` is its own quoting style. If you nest `$''` syntax inside double quotes, you're in a double-quoted context, not a `$''`-quoted context. – Charles Duffy Sep 26 '18 at 21:36
  • Also, note that `set -x` is perfectly legal to take content written in one quoting style and echo it back in a different quoting style, so long as the underlying string being represented (and thus, passed to the program invoked) is the same. To phrase it a bit differently, the representation used by `set -x` has no impact on how `curl` sees the world, whether that's double-quoted, single-quoted, `$''`-quoted, etc. – Charles Duffy Sep 26 '18 at 21:37
  • ...as another aside, note that `set -e` is quite controversial -- its behavior is often surprising, and can cause more problems than it solves (f/e, it can change how a function behaves *depending on whether any other code is branching based on that function's return code*); and that's before one gets into version incompatibilities caused by places where its behavior changed over time. See [the exercises in BashFAQ #105](http://mywiki.wooledge.org/BashFAQ/105#Exercises) (and the various links from that page) to be sure you know what you're getting into. – Charles Duffy Sep 26 '18 at 21:42
  • Moral to this story: don't trust `set -ex`. The answer below looks wrong using `set -ex` but will work – cchuter Sep 27 '18 at 15:58

1 Answers1

3

The single quotes are just how trace mode (from the -x option) displays the string; it doesn't affect your command at all. The main problem is that you are combining two separate arguments (--form and its argument) into one.

The correct script should be something like

set -ex

curl_headers=(
    --silent
    --form
    release_description=$'\n new \n'
)

curl "${curl_headers[@]}" "example.com"
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 1
    ...one *could*, of course, put `--form release_description=$'\n new \n'` on the same line; I typically would, to group options and their optargs. What's important is that they aren't quoted as part of a single string. – Charles Duffy Sep 26 '18 at 21:40
  • What value do you want for the release description? If you want the literal string `\n new \n`, then use `release_description='\n new \n'`. That's different from what you claimed to be the desired result in the question, though. – chepner Sep 27 '18 at 15:23