如何在 bash 数组元素中转义引号



我想创建一个这样的数组

array=(
"element1 with "quoted string""
"element2 without double quoted string"
"element3"
)

运行代码后,它输出

echo ${array[0]}                                                               
element1 with quoted.

我正在尝试在回显输出中包含引号。我该怎么做?

要么使用单引号,

array=(
'element1 with "quoted string"'
...
)

或转义文字双引号:

array=(
"element1 with "quoted string""
...
)

您的第一个嵌套引号关闭了开头引号,但quoted仍被视为当前单词的一部分。数组的下一个元素string,其末尾附加一个空字符串。

最新更新