[ad_1]
We can assign a variable for curl using single quote '
and wrap some other variables in
- a single quote =>
'
$variable'
- a double-quote single-quote =>
"'
$variable'"
- a single+double+single quote =>
'"'
$variable'"'
Lets test each case, but first watch out for this catch that if we use a single quote '
for variable assignment, that variable is not evaluated.
watch out
Please notice the assignment is done by a single quote CURL_DATA='content'
cmd='ls'
CURL_DATA='{
"cmd": "$cmd", <===== our variable
"args": [ "-la" , "/tmp" ],
"options": {
"cwd": "/tmp"
},
"type": "sync"
}';
echo "$CURL_DATA";
Will give us
{
"cmd": "$cmd", <===== we need ls not $cmd
"args": [ "-la" , "/tmp" ],
"options": {
"cwd": "/tmp"
},
"type": "sync"
}
a single quote '
$variable '
- The value of a variable is evaluated
- neither single quote
'
nor applies double one"
cmd='ls'
CURL_DATA='{
"cmd": '$cmd', <===== our variable
"args": [ "-la" , "/tmp" ],
"options": {
"cwd": "/tmp"
},
"type": "sync"
}';
echo "$CURL_DATA";
Will give us:
{
"cmd": ls, <===== neither 'ls' nor "ls", just ls
"args": [ "-la" , "/tmp" ],
"options": {
"cwd": "/tmp"
},
"type": "sync"
}
a double-quote single-quote "'
$variable '"
- variable is evaluated
- will be surrounded by a double quote
"
cmd='ls'
CURL_DATA='{
"cmd": "'$cmd'", <===== our variable
"args": [ "-la" , "/tmp" ],
"options": {
"cwd": "/tmp"
},
"type": "sync"
}';
echo "$CURL_DATA";
Will give us
{
"cmd": "ls", <===== we have double quote " variable "
"args": [ "-la" , "/tmp" ],
"options": {
"cwd": "/tmp"
},
"type": "sync"
}
a single+double+single quote => '"'
$variable '"'
- variable is evaluated
- will be surrounded by a single quote
'
cmd='ls'
CURL_DATA='{
"cmd": '"'$cmd'"',
"args": [ "-la" , "/tmp" ],
"options": {
"cwd": "/tmp"
},
"type": "sync"
}';
echo "$CURL_DATA";
Will give us
{
"cmd": 'ls', <===== we have a single quote ' variable '
"args": [ "-la" , "/tmp" ],
"options": {
"cwd": "/tmp"
},
"type": "sync"
}
summary
# no quote at all
$cmd => $cmd
# a double quote (" $variable ")
"$cmd" => "$cmd"
# a single quote (' $variable ')
'$cmd' => ls
# a single quote + a double quote ("' $variable '")
"'$cmd'" => "ls"
# a single-double-single quote ('"' $variable '"')
'"'$cmd'"' => 'ls'
which one we should use?
Since JSON
needs a double quote "
for its key or value we can use :
- a double-quote single-quote
"'
$variable'"
curl
cmd='ls'
CURL_DATA='{
"cmd": "'$cmd'",
"args": [ "-la" , "/tmp" ],
"options": {
"cwd": "/tmp"
},
"type": "sync"
}';
echo "$CURL_DATA" | jq '.'
curl --data "$CURL_DATA" -X POST localhost:3232/cmd | jq '.'
NOTE:
The equivalent of '
for a variable evaluation is '"
which means instead of using '$cmd'
we can use '"$cmd"'
and it gives us ls
neither with a single quote nor a double quote , but it gets more confusing if we needed to apply for curl since we need a double quoted result "ls"
and would have to wrap it in another double quote => "'"
This code works well, but the above is more readable
cmd='ls'
CURL_DATA='{
"cmd": "'"$cmd"'", <===== our variable
"args": [ "-la" , "/tmp" ],
"options": {
"cwd": "/tmp"
},
"type": "sync"
}';
echo "$CURL_DATA" | jq '.'
curl --data "$CURL_DATA" -X POST localhost:3232/cmd | jq '.'
Will give us:
{
"cmd": "ls", <===== result
"args": [ "-la" , "/tmp" ],
"options": {
"cwd": "/tmp"
},
"type": "sync"
}
finally
We can use either of:
"cmd": "'$cmd'", <===== will be: "ls"
or
"cmd": "'"$cmd"'", <===== will be: "ls"
and "$CURL_DATA"
as a normal variable
curl --data "$CURL_DATA" -X POST localhost:3232/cmd
[ad_2]