Original string getting mutated while printing with echo statement.
#!/bin/bash
response='{\\\"test\\\":\\\"data\\\"}'
echo $response;
Actual Output - {\\"test\\":\\"data\\"}
Expected output - {\\\"test\\\":\\\"data\\\"}
Original string getting mutated while printing with echo statement.
#!/bin/bash
response='{\\\"test\\\":\\\"data\\\"}'
echo $response;
Actual Output - {\\"test\\":\\"data\\"}
Expected output - {\\\"test\\\":\\\"data\\\"}
printf
, not echo
(see https://unix.stackexchange.com/q/65803/133219)e.g.:
$ response='{\\\"test\\\":\\\"data\\\"}'
$ printf '%s\n' "$response"
{\\\"test\\\":\\\"data\\\"}
This works as expected in bash, but you are instead running it with sh
. See: Why does my bash code fail when I run it with sh?
However, when you want to print a string exactly as is, use printf
:
response='{\\\"test\\\":\\\"data\\\"}'
printf '%s\n' "$response"
This works correctly for all values in all shells, including response='*'
reponse='-n'
and response='foo bar'