首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在bash脚本中传递curl命令中的变量

如何在bash脚本中传递curl命令中的变量
EN

Unix & Linux用户
提问于 2022-10-29 08:28:32
回答 1查看 3K关注 0票数 0

我正在创建一个bash脚本,在该脚本中,我需要将变量$matchteams &$匹配时间传递到以下curl命令

代码语言:javascript
运行
复制
curl -X POST \
  'http://5.12.4.7:3000/send' \
  --header 'Accept: */*' \
  --header 'User-Agent: Thunder Client (https://www.thunderclient.com)' \
  --header 'Content-Type: application/json' \
  --data-raw '{
  "token": "abcdjbdifusfus",
  "title": "$matchteams | $matchtime",
  "msg": "hello all",
  "channel": "1021890237204529235"
}'

有人能帮帮我吗?

EN

回答 1

Unix & Linux用户

回答已采纳

发布于 2022-10-29 12:37:43

单引号中的文本被视为文字:

代码语言:javascript
运行
复制
--data-raw '{
  "token": "abcdjbdifusfus",
  "title": "$matchteams | $matchtime",
  "msg": "hello all",
  "channel": "1021890237204529235"
}'

(变量周围的双引号也被视为文本。)在这种情况下,您需要从单引号中取出变量,以便由shell解析和展开变量,或者将整个字符串括在双引号中,适当地转义文字双引号:

代码语言:javascript
运行
复制
# Swapping between single quote strings and double quote strings
--data-raw '{
  "token": "abcdjbdifusfus",
  "title": "'"$matchteams | $matchtime"'",
  "msg": "hello all",
  "channel": "1021890237204529235"
}'

# Enclosing the entire string in double quotes with escaping as necessary
--data-raw "{
  \"token\": \"abcdjbdifusfus\",
  \"title\": \"$matchteams | $matchtime\",
  \"msg\": \"hello all\",
  \"channel\": \"1021890237204529235\"
}"

请记住,"abc"'def'是以abcdef的形式由shell展开的,所以交换引用样式为中间字符串是可以接受的。总的来说,我倾向于使用第一种风格。

票数 3
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/722906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档