卷曲带括号/支架有问题

问题描述 投票:0回答:1

我想执行的sh文件下面一行:

test2='{"admins":{"names":["superuser"],"roles":["admins", "test"]}}'
curl $url -X PUT -H 'content-type: application/json' -H 'accept: application/json' -d $test2

它会给我这样的错误

curl: (3) [globbing] unmatched close brace/bracket in column 7

然而,当我不喜欢这个shell变量运行命令

curl $url -X PUT -H 'content-type: application/json' -H 'accept: application/json' -d '{"admins":{"names":["superuser"],"roles":["admins", "test"]}}'

它工作正常。

这里发生了什么。很是不解。

谢谢您的帮助

shell curl escaping quotes curly-braces
1个回答
2
投票

试试这个(转义双引号,并使用"$test2"代替)

test2="{\"admins\":{\"names\":[\"superuser\"],\"roles\":[\"admins\", \"test\"]}}"
curl $url -X PUT -H 'content-type: application/json' -H 'accept: application/json' -d "$test2"
© www.soinside.com 2019 - 2024. All rights reserved.