您可以使用here-doc:
foo=$(cat <<EOF{"Comment":"Update DNSName.","Changes":[{"Action":"UPSERT","ResourceRecordSet":{"Name":"alex.","Type":"A","AliasTarget":{"HostedZoneId":"######","DNSName":"$bar","evaluateTargetHealth":false}}}]}EOF)
通过
EOF在第一行不加引号,here-doc的内容将进行参数扩展,因此您的
$bar扩展内容将扩展为您在其中输入的内容。
如果您可以在JSON中使用换行符,则可以使其更具可读性:
foo=$(cat <<EOF{ "Comment": "Update DNSName.", "Changes": [ { "Action": "UPSERT", "ResourceRecordSet": { "Name": "alex.", "Type": "A", "AliasTarget": { "HostedZoneId": "######", "DNSName": "$bar", "evaluateTargetHealth": false } } } ]}EOF)
甚至(每行的第一个缩进必须是制表符)
foo=$(cat <<-EOF { "Comment": "Update DNSName.", "Changes": [ { "Action": "UPSERT", "ResourceRecordSet": { "Name": "alex.", "Type": "A", "AliasTarget": { "HostedZoneId": "######", "DNSName": "$bar", "evaluateTargetHealth": false } } } ] } EOF)
并显示其存储方式,包括引用(假设
bar=baz):
$ declare -p foodeclare -- foo="{ "Comment": "Update DNSName.", "Changes": [ { "Action": "UPSERT", "ResourceRecordSet": { "Name": "alex.", "Type": "A", "AliasTarget": { "HostedZoneId": "######", "DNSName": "baz", "evaluateTargetHealth": false } } } ]}"
因为这扩展了一些外壳元字符,
``所以如果您的JSON包含类似的内容,您可能会遇到麻烦,因此,您可以直接分配,但要谨慎引用$bar`:
foo='{"Comment":"Update DNSName.","Changes":[{"Action":"UPSERT","ResourceRecordSet":{"Name":"alex.","Type":"A","AliasTarget":{"HostedZoneId":"######","DNSName":"'"$bar"'","evaluateTargetHealth":false}}}]}'
请注意
$bar:
"'"$bar"'"│││ ││││││ ││└ literal double quote│││ │└ opening syntactical single quote│││ └ closing syntactical double quote││└ opening syntactical double quote│└ closing syntactical single quote└ literal double quote
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)