简体   繁体   English

jQuery变量添加额外的\\

[英]Jq variable add extra \

Hi i have this bash code 嗨,我有这个bash代码

#!/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 "\\"? 为什么jq加和额外的“ \\”? What im 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 谢谢

\\n does not mean linefeed/newline in a bash double quoted string. \\n表示bash双引号字符串中的换行/换行符。 It's just backslash+lowercase n. 只是反斜杠+小写字母n。

If you use linefeeds instead of backslashes and Ns, they will encode the way you want: 如果您使用换行符而不是反斜杠和Ns,它们将以您希望的方式进行编码:

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: 以下是将文字换行符放入bash变量的其他等效方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM