删除 bash 中数组中所有字段的双引号

use*_*852 2 bash awk sed

该数组存在于最小 linux/posix 环境下的 bash 脚本中。我的字符串数组中的所有字段都用双引号引起来。我正在寻求一种优雅的解决方案来删除每个字段开头和结尾的双引号字符,因为字段中可能存在不应删除的双引号。

该数组是一维的,包含如下字段:

"This is a value, in this element"
"This is also a "value" but has double quotes"
"0X:41:DE:AD:BE:EF; -- EXIT --"
Run Code Online (Sandbox Code Playgroud)

操作后所需的字段值如下:

This is a value, in this element
This is also a "value" but has double quotes
0X:41:DE:AD:BE:EF; -- EXIT --
Run Code Online (Sandbox Code Playgroud)

目前我已尝试以下方法但没有成功:

fields=`sed -e 's/^"//' -e 's/"$//' <<<"${fields[@]}"
Run Code Online (Sandbox Code Playgroud)

And*_*mek 9

假设 \xe2\x80\x9cin Bash\xe2\x80\x9d 表示 \xe2\x80\x9c 没有外部进程\xe2\x80\x9d,您可以将通常的 Bash 扩展/转换应用于每个数组元素。这会产生所需的输出,例如:

\n
fields=(\'"This is a value, in this element"\'\n        \'"This is also a "value" but has double quotes"\'\n        \'"0X:41:DE:AD:BE:EF; -- EXIT --"\')\n        \nfields=("${fields[@]/#\\"}")  # remove leading quotes\nfields=("${fields[@]/%\\"}")  # remove trailing quotes\n\nprintf \'%s\\n\' "${fields[@]}"\n
Run Code Online (Sandbox Code Playgroud)\n