开发者

expanding variable in CURL [duplicate]

开发者 https://www.devze.com 2023-01-24 07:00 出处:网络
This question already has an answer here: Bash: variable insertion into curl call not working [duplicate]
This question already has an answer here: Bash: variable insertion into curl call not working [duplicate] (1 answer) Closed 4开发者_如何学C years ago.

The following curl statement works OK in my shell script.

curl -Ld'username=user&password=password&source=oksoft&dmobile='$mnumber'&message=Slave is working OK' http://167.123.129.195/smsclient//api.php

The Mobile number is expanded as expected. But when I use a variable for the message parameter, I get $myvar output instead of the expanded variable.

curl -Ld'username=user&password=password&source=oksoft&dmobile='$mnumber'&message="$myvar"' http://167.123.129.195/smsclient//api.php

How do I use the variable for the message?


Single quotes inhibit expansion. Switch up the quotes.

curl ...'..."'"$myvar"'"...'...


$myvar is wrapped in single quotes, $mnumber is not. It doesn't matter that the innermost quotes are double, it's the outermost quotes that the shell uses to determine if it should do variable expansion or not.

Rewrite it like this (I shortened the URLs to make it readable):

curl -Ld'...dmobile='$mnumber'&message="'$myvar'"' http://...

Notice the extra single quotes before and after $myvar.


It depends on the shell you are running. "$var" will work on csh. Under windows you should use the "%" notation.

0

精彩评论

暂无评论...
验证码 换一张
取 消