Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 241153
Next
Alex Hales
  • 0
Alex HalesTeacher
Asked: August 10, 20222022-08-10T01:13:01+00:00 2022-08-10T01:13:01+00:00In: Bash, curl, javascript-objects, JSON

json – Using curl POST with variables defined in bash script functions

  • 0

[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]

  • 0 0 Answers
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 325 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 295 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 292 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.