Jess
Jess

Reputation: 21

Jq variable add extra \

Hi i have this bash code

#!/bin/bash
textb="\n 1 \n 2 \n 3 \n 4"
jq  --arg textb "$textb" '. | {plain_text: (  $textb +  desc.envi )'

When i run the comand this giveme the next example

 #!/bin/bash
    \\n1 \\n2 \\n3 \\n4

Why jq add and extra "\"? What i m going wrong? I try some like this

textb="\n" 1 "\n" 2 "\n" 3 "\n" 4"

But i have this result

n1 n2 n3 n4

Thx

Upvotes: 0

Views: 93

Answers (1)

that other guy
that other guy

Reputation: 123440

\n does not mean linefeed/newline in a bash double quoted string. It's just backslash+lowercase n.

If you use linefeeds instead of backslashes and Ns, they will encode the way you want:

textb="
1
2
3
4"
jq -n --arg textb "$textb" '."the string is:" = $textb'

outputs:

{
  "the string is:": "\n1\n2\n3\n4"
}

Here are few other equivalent ways of putting literal linefeeds into a bash variable:

textb=$'\n1\n2\n3\n4'
textb=$(printf '\n%s' {1..4})

Upvotes: 1

Related Questions