You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何向Shell脚本传入Bearer Token无效但直接嵌入却可行?

问题原因

你脚本里的Authorization头用了单引号包裹,Shell中单引号内的变量不会被解析,所以curl实际发送的是字符串Bearer $1,而不是你传入的token值,这就导致了未授权错误。

解决方案

把Authorization头的单引号替换成双引号,让Shell能解析$1变量:

curl -i -X GET 'https://example.com/bar' 
    -H "Authorization: Bearer $1"

或者也可以用单引号拼接变量的方式(适合需要保留部分单引号内容的场景):

curl -i -X GET 'https://example.com/bar' 
    -H 'Authorization: Bearer '"$1"
额外提示

调用脚本时不需要用.(source命令),直接执行即可:

./foo.sh abc123def456

如果用Python脚本生成token,直接这样调用也没问题:

./foo.sh $(python -c "import get_token; print(get_token.get_token())")

内容的提问来源于stack exchange,提问作者shearichard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 03:05:53