[英]How can I use an argument in a string in a bash function
I try: 我尝试:
ctests() {
curl -X POST \
http://route.to.host/cucumber/execute-tests \
-H 'Authorization: Basic xxxxxxxxxxxxxxxxxxxxx' \
-H 'Content-Type: application/json' \
-H 'cache-control: no-cache' \
-d '{ "text": "cucumber! alltests products=$1" }'
}
And want to call this like 并希望这样称呼
> ctests someproduct
But $1 wont resolve. 但是1美元不会解决。 I tried ${1}, but its the same. 我尝试了$ {1},但还是一样。 Is there a nice solution for this? 有一个好的解决方案吗?
the $1
doesn't resolve because you are using single-ticks '
which prohibit variable resolution. $1
不能解析,因为您使用的是单行号'
,它禁止变量解析。
use double-ticks ( "
) instead (you'll have to escape the double-quotes inside the double-quotes; or use single-quotes within the double-quotes; depending on your context) 请改用双引号( "
)(您必须在双引号内转义双引号;或在双引号内使用单引号;具体取决于您的上下文)
ctests() {
curl -X POST \
http://route.to.host/cucumber/execute-tests \
-H 'Authorization: Basic xxxxxxxxxxxxxxxxxxxxx' \
-H 'Content-Type: application/json' \
-H 'cache-control: no-cache' \
-d "{ \"text\": \"cucumber! alltests products=$1\" }"
}
quoting bash(1)
: 引用bash(1)
:
QUOTING 报价
[...] [...]
Enclosing characters in single quotes preserves the literal value of each character within the quotes. 将字符括在单引号中可保留引号内每个字符的字面值。 A single quote may not occur between single quotes, even when preceded by a backslash. 即使在单引号之前加反斜杠,也不能在单引号之间引起单引号。
Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $ [...] 用双引号引起来的字符保留引号内所有字符的文字值,但$ [...]除外
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.