I need set into variable true/false value and send him into json with curl command:


`curl -d '{"name":"'"$name"'", "sex":true}' -H "Content-Type: application/json" -X POST http://localhost:8080/setacc`

Variable sx accepts the value male or female how to set the appropriate boolean value into sex variable?

  • 1
    Which one is the true sex? Male or female? Maybe there are other values?
    – PerlDuck
    Nov 16 '18 at 19:50
  • @PerlDuck it is do not matter. It is can be 0 - true, or 1 - false Nov 16 '18 at 19:52
  • 4
    The USA is awake and they're a bunch of puritans. A bit later New Zealand and Australia will wake up and they've got a great sense of humour, so they'll give you a sympathy +1 as well.
    – Fabby
    Nov 16 '18 at 20:48
  • 1
    Naah, never mind! I think the question was just downvoted because of the mixture of male/female, true/false, and 0/1.
    – PerlDuck
    Nov 16 '18 at 20:49
  • 1
    "sex":file-not-found Nov 17 '18 at 0:04

The easiest way to map the string male to either true or false (depending on your needs) and female to some other value is a simple if…then…else clause. The trick is the quoting but you already got that with the $name variable. So:

if [ "$sx" = "male" ]; then
    sex="true";     # or whatever you consider male sex to be
    sex="false";    # just the opposite, see above

curl -d '{"name":"'"$name"'", "sex":'$sex'}' -H "Content-Type: application/json" -X POST http://localhost:8080/setacc

In your case you use json and can use text "true" and text "false".

You need add to bash script logic:

if [ "$sx" = "male" ]; then

then run curl command with ... {"name":"'"$name"'", "sex":\"$sex\"} ... or just ... {"name":"'"$name"'", "sex":$sex} ...

Test script:



if [ "$sx" = "male" ]; then

echo \"$sex\"

Using a variable for the whole JSON string and building it piece wise can make the quoting less cumbersome and make the curl command line more readable and easier to maintain even though the overall code is much more verbose.

Building the string is done here using the string self concatenation operator +=. Note that in json_string+=$name, for example, no quoting is necessary since there's no word splitting being performed and no special characters are present on the right hand side.

You can use an associative array to look up the value you want based on the provided key. Here I'm assigning the pairs individually. Below I'll show how to do them in one assignment.


declare -A sexes

json_string+='", "sex":'

curl -d "$json_string" " -H "Content-Type: application/json" -X POST http://localhost:8080/setacc

You can break the JSON up even more to make it look a little more structured in the code (the resulting string contents will still be a single-line string). Ideally, if you're using more complicated JSON than this you should use a purpose built JSON tool for building the structure.

Here's how you can make the associative array declaration and assignment all at once. The first example is on one line and the second is on multiple lines - again for improved readability and maintainability.

declare -A sexes='([female]="true" [male]="false")'

declare -A sexes='(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.