0

I have a string containing quotes and backslahes:

-options \'{"version": "http"}\'

I would like to initialize a variable PARAM with this string. How this can be done in bash?

I thought of adding it to an array: PARAMS=(-options \'{"version": "http"}\')

but the output I am getting is: -options '{version: http}' i.e. without the slashes.

Expected output: -options \'{"version": "http"}\'

Can someone please suggest?

Ira
  • 547
  • 4
  • 13
  • What are you going to use it for exactly? Is it supposed to be one string or two separate arguments for a command? Beware the [XY problem](https://meta.stackexchange.com/q/66377/343832). At first glance it seems like this isn't a duplicate of [the question Barmar closed under](/q/13365553/), but I can't tell what you're actually trying to do. – wjandrea Sep 10 '22 at 00:41
  • I am trying to append `-options \'{"version": "http"}\'` to an existing line in a file. Yes, it is a single string containing a parameter `-version` and its value `\'{"version": "http"}\'`. – Ira Sep 10 '22 at 00:47
  • You don't need to include the single quotes as literal characters, unless you plan on passing the string to `eval` eventually (which is probably a bad idea in the first place).`PARAMS=(-options '{"version": "http"}')` should suffice. Later, you'll run some command like `foo "${PARAMS[@]"`; the JSON object will be preserved as a single object without the need to provide syntactic quotes around it. – chepner Sep 10 '22 at 14:24

1 Answers1

1

This looks ok to me.

test="-client-options \\'{\"quic-version\": \"h3\"}\\'"
echo "$test"
t2=("$test" "etc")
echo ${t2[@]}

Escape every inner " and double escape for a persisting escape

Zaira
  • 42
  • 2