问题描述
我发布了问题
I posted a question here and got an answer that gave me what I needed.
我有一个类似的问题,但是我找不到答案,所以我在问另一个:/
I have a similar problem, but one that I can't find answer for so I'm asking another :/
当我直接在终端中运行以下命令时,通过管道连接到jq时,我会得到正确的响应。
When I run the following command directly in the terminal, I get the correct response when piped into jq.
curl GET "https://jsonplaceholder.typicode.com/posts/2" | jq '.body'
RESPONSE: "*est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla*"
但是,当我在可执行的bash脚本中运行相同的命令时,看到没有回应?
However, when I run the same command inside an executable bash script, I see no response?
result="$(curl GET https://jsonplaceholder.typicode.com/posts/2)" | jq '.body'
echo "$result"
回显的结果是一个空字符串
The echoed result is an empty string.
请问有人可以指出正确的方向,并用双引号或单引号突出显示某些细微差别吗?我假设我犯了语法错误?
Would someone be able to point me in the right direction please, and possibly highlight some of the nuances with double/single quotations etc? I'm assuming I've making a syntax error?
谢谢
推荐答案
您应该在子外壳中的jq中处理结果。
You should process the results in jq inside the subshell.
result=$(curl GET https://jsonplaceholder.typicode.com/posts/2 | jq '.body')
echo "$result"
@ 123首先在评论中指出
first pointed out by @123 in a comment
这篇关于使用Bash和cURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!