首页 > 解决方案 > 传递的参数不会在 curl 命令中解释

问题描述

我有一个 shell 脚本,它只执行 curl 命令来通知我的 Discord 服务器有关问题。我传递了两个参数,我想将它们添加到 curl 命令的数据体中,但它们的值不会被解释。

我的脚本调用:

./notifier.sh usernameArg contentArg

我的脚本:

#!/bin/bash

curl -X POST -H "Content-Type: application/json" -d '{"username": "$1", "content": "$2"}' "https://<discord webhook url>"

但它发送的只是{"username": "$1", "content": "$2"}. 我还尝试使用 ${1} 和 ${2} 获得相同的结果。

提前感谢您的帮助!

标签: bashsh

解决方案


试试这个。

#!/bin/bash

curl -X POST -H "Content-Type: application/json" -d '{"username": "'$1'", "content": "'$2'"}' "https://<discord webhook url>"

推荐阅读