Nick N
Nick N

Reputation: 59

When creating a variable from command output, Bash removes a backslash from the JSON. How do I make it keep both backslashes to maintain valid JSON?

I'm doing the following to capture some ADO JSON data:

iteration="$(az boards iteration team list --team Test --project Test --timeframe current)"

Normally, the output of that command contains a JSON key/value pair like the following:

"path": "Test\\Sprint1"

But after capturing the STDOUT into that iteration variable, if I do

echo "$iteration"

That key/value pair becomes

"path": "Test\Sprint1"

And if I attempt to use jq on that output, it breaks because it's not recognized as valid JSON any longer. I'm very unfamiliar with Bash. How can I get that JSON to remain valid all the way through?

Upvotes: 0

Views: 86

Answers (1)

Socowi
Socowi

Reputation: 27225

As already commented by markp-fuso:

It looks like your echo command is interpreting the backslashes. You can confirm this by running echo 'a\\b' and looking at the output.

The portable way to deal with such problems is to use printf instead of echo:

printf %s\\n "$iteration"

Upvotes: 5

Related Questions