Jq变量添加额外的\

问题描述 投票:0回答:1

嗨,我有这个bash代码

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

当我运行命令时,这给我下一个例子

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

为什么jq添加和额外的“\”?我错了什么?我试试这样的

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

但我有这个结果

n1 n2 n3 n4

谢谢

json bash shell jq backslash
1个回答
1
投票

\n并不是指bash双引号字符串中的换行符/换行符。它只是反斜杠+小写n。

如果您使用换行而不是反斜杠和Ns,它们将按您希望的方式编码:

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

输出:

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

以下是将文字换行放入bash变量的其他几种等效方法:

textb=$'\n1\n2\n3\n4'
textb=$(printf '\n%s' {1..4})
© www.soinside.com 2019 - 2024. All rights reserved.