如何仅在 shell 中删除引号?

How to only do quote removal in a shell?

给定一个来自不受信任来源的字符串,例如

MALICIOUS_INPUT="$(awk -F = '/^VERSION=/ {print }' /path/to/compromised/etc/os-release | head -n 1)"

是否可以仅对该字符串应用纯粹的 shell 引号删除(参见 Shell Command Language (IEEE Std 1003.1-2017) and Bash manual),即不进行变量扩展、算术扩展、命令替换和类似操作?

这是必需的,例如,无需 source-ing 文件即可解析来自 os-release 文件的字符串。

Input Expected result
'\"' \"
"\"" "
'$foo${foo}$(pwd)$((1+2))' $foo${foo}$(pwd)$((1+2))
"$foo${foo}$(pwd)$((1+2))" $foo${foo}$(pwd)$((1+2))

比较 Reading quoted/escaped arguments correctly from a string 上已有答案对这个问题的适用性:

parse_with_xargs() {
  xargs printf '%s[=10=]' <<<"$*"
}

parse_with_python() {
  python -c '
import shlex, sys
for item in shlex.split(sys.stdin.read()):
    sys.stdout.write(item + "[=10=]")
' <<<"$*"
}

readarray -t example_lines <<'EOF'
'\"'
"\""
'$foo${foo}$(pwd)$((1+2))'
"$foo${foo}$(pwd)$((1+2))"  
EOF

for line in "${example_lines[@]}"; do
  printf 'Input line:         %s\n' "$line"
  printf 'Parsed with xargs:  '; parse_with_xargs "$line" 2>&1; echo
  printf 'Parsed with python: '; parse_with_python "$line" 2>&1; echo
  echo
done

输出:

Input line:         '\"'
Parsed with xargs:  \"
Parsed with python: \"

Input line:         "\""
Parsed with xargs:  xargs: unmatched double quote; by default quotes are special to xargs unless you use the -0 option

Parsed with python: "

Input line:         '$foo${foo}$(pwd)$((1+2))'
Parsed with xargs:  $foo${foo}$(pwd)$((1+2))
Parsed with python: $foo${foo}$(pwd)$((1+2))

Input line:         "$foo${foo}$(pwd)$((1+2))"  
Parsed with xargs:  $foo${foo}$(pwd)$((1+2))
Parsed with python: $foo${foo}$(pwd)$((1+2))