将字符串而不是文件名传递给 bash 下 jq 中的 -f 选项

Passing string rather than filename to -f option in jq under bash

我很好奇在使用 -f 选项 [=16] 时是否可以将 文件名 替换为 字符串 =].

当我做的时候

$ jq -nf <(echo \"hello world\")

它正确输出

"hello world"

但是当我尝试

$ jq -nf <<<$'\"hello world\"'

失败并出现错误

jq: Could not open .: It's a directory

任何人都知道如何使这项工作?我知道这更像是一个 bash 问题而不是 jq.

$ jq '.' <<< '"hello world"' 虽然有效。

谢谢。

您可以使用/dev/stdin将标准输入表示为文件:

jq -nf /dev/stdin <<<$'\"hello world\"'
echo \"hello world\" | jq -nf /dev/stdin