2

I have something like this:

A=$(curl https://mysite.com)

and the curl request returns the string \"Hello World\". When I now want to print A to the console using one of:

echo "$A"
printf '%s' "$A"

the \ dissappear and it just says "Hello World". What can I do to get \"Hello World\" on the console?

1
  • 3
    If printf '%s' "$A" doesn't show backslashes, then they're not there. What's the output of typeset -p A? Nov 30, 2023 at 13:46

1 Answer 1

5

If printf '%s' "$A" doesn't show any backslash then they're not there. You can check with curl https://mysite.com alone.

Maybe you were confused by the output of:

bash-5.0$ typeset -p A
declare -- A="\"Hello World\""

That's bash outputting shell code that could be used to define that $A variable. In the syntax of the shell language, the \ is there to escape the following " to tell the shell that that " is part of the data and not the closing quote.

It could also have output:

declare -- A='"Hello World"'

Which does just same (and is safer). Or A=\"Hello\ World\" or $'"Hello World"', etc which are other forms of quoting in the shell language syntax.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .