为什么将带有文字引号的字符串传递给 xargs 会使它们消失?

Why does piping a string with literal quotes to xargs make them disappear?

我正在编写 bash 包含链式 printf 管道的脚本。 (成像构建 JSON 个对象)

我的用法是这样的:
printf \"abc\" | xargs printf
结果是 abc 不带引号。如果我想在第二个 printf 输出中保留引号,我需要做什么?

这是 xargs 所记录的行为;它实现了 shell 类(但不严格 shell 兼容)解析和引用删除。

GNU 和 BSD 平台都有超出 POSIX 基线的扩展,可让您禁用此功能。

在具有 GNU 或 BSD 扩展的系统上,使用 -0 并用 NUL 分隔您的输入:

printf '%s[=10=]' \"abc\" | xargs -0 printf

使用 GNU xargs,您可以使用 -d 指定要使用的定界符 -- 换行符有效的上下文:

printf '%s\n' \"abc\" | xargs -d $'\n' printf