我花了很长时间来解开这个谜题。
我有一个名为 API_ID 的 shell 变量,我使用 jq 从 json 中解析了它
ludo$ echo $API_ID
> "ucjj72orp1"
为什么当我这样复制粘贴时它工作正常:
aws apigateway get-resources --rest-api-id "ucjj72orp1"
像这样我得到一个错误:
aws apigateway get-resources --rest-api-id $API_ID
An error occurred (NotFoundException) when calling the GetResources operation: Invalid REST API identifier specified 420257254224:"ucjj72orp1"
最佳答案
您需要在jq
命令中使用-r
选项。这允许在没有双引号的情况下获取原始数据。
如果您设置 myvar=$(jq '...')
,该变量将被设置为包含双引号的 shell 字符串。
如果您在内联 shell 命令中复制/粘贴字符串,shell 会将双引号解释为字符串分隔符,因此它会起作用。
关于linux shell变量到aws命令错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50464279/