我有这个变量
TOKEN="Authorization: Bearer eyJ0eXA"
当我将其添加到命令curl 时
curl -H "$TOKEN" GET 'https://x.x.x.x/service/' --header 'Accept-Language: en'
当运行时,它会替换">在-H之后到'以使命令类似
curl -H 'Authorization: Bearer eyJ0eXA' GET https://x.x.x.x/service/ --header 'Accept-Language: en'
如何避免这种情况,并使命令与"strong"一起运行
没有可替换的引号。它们已经被外壳移除:
TOKEN="Authorization: Bearer eyJ0eXA"
echo $TOKEN
Authorization: Bearer eyJ0eXA
如果你想在你的字符串周围有双引号,那么改为:
TOKEN='"Authorization: Bearer eyJ0eXA"'
echo $TOKEN
"Authorization: Bearer eyJ0eXA"
我猜您看到的是set -x
或其他shell调试的结果。
在这种情况下,这是完全正确的。$TOKEN
已被评估并替换为其值。单引号向shell指示不需要对该值进行进一步处理。它们既不会被curl
看到,也不会传递到目标主机。