我正在创建一个bash脚本,在该脚本中,我需要将变量$matchteams &$匹配时间传递到以下curl命令
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"
}'
有人能帮帮我吗?
发布于 2022-10-29 12:37:43
单引号中的文本被视为文字:
--data-raw '{
"token": "abcdjbdifusfus",
"title": "$matchteams | $matchtime",
"msg": "hello all",
"channel": "1021890237204529235"
}'
(变量周围的双引号也被视为文本。)在这种情况下,您需要从单引号中取出变量,以便由shell解析和展开变量,或者将整个字符串括在双引号中,适当地转义文字双引号:
# 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展开的,所以交换引用样式为中间字符串是可以接受的。总的来说,我倾向于使用第一种风格。
https://unix.stackexchange.com/questions/722906
复制相似问题