How to set variable in the curl command in bash?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite












I have a bash file:



#!/bin/bash

# yesnobox.sh - An inputbox demon shell script
OUTPUT="/tmp/input.txt"

# create empty file
>$OUTPUT

# cleanup - add a trap that will remove $OUTPUT
# if any of the signals - SIGHUP SIGINT SIGTERM it received.
trap "rm $OUTPUT; exit" SIGHUP SIGINT SIGTERM

# show an inputbox
dialog --title "Inputbox"
--backtitle "Search vacancies"
--inputbox "Enter your query " 8 60 2>$OUTPUT

# get respose
respose=$?

# get data stored in $OUPUT using input redirection
name=$(<$OUTPUT)

curl -d '"query":"developer", "turnOff":true' -H "Content-Type: application/json" -X POST http://localhost:8080/explorer


in last string (curl command) I want to set variable name instead "developer". How to correctly insert it?










share|improve this question



















  • 1




    Sidenote: you can create a temporary file like so: OUTPUT=$(mktemp). It will create a unique file (sth. like /tmp/tmp.RyR3UlsV5c). However, you will still need to delete it manually, like you already do. mktemp creates such a file and returns (prints) its filename.
    – PerlDuck
    Aug 31 at 12:55














up vote
4
down vote

favorite












I have a bash file:



#!/bin/bash

# yesnobox.sh - An inputbox demon shell script
OUTPUT="/tmp/input.txt"

# create empty file
>$OUTPUT

# cleanup - add a trap that will remove $OUTPUT
# if any of the signals - SIGHUP SIGINT SIGTERM it received.
trap "rm $OUTPUT; exit" SIGHUP SIGINT SIGTERM

# show an inputbox
dialog --title "Inputbox"
--backtitle "Search vacancies"
--inputbox "Enter your query " 8 60 2>$OUTPUT

# get respose
respose=$?

# get data stored in $OUPUT using input redirection
name=$(<$OUTPUT)

curl -d '"query":"developer", "turnOff":true' -H "Content-Type: application/json" -X POST http://localhost:8080/explorer


in last string (curl command) I want to set variable name instead "developer". How to correctly insert it?










share|improve this question



















  • 1




    Sidenote: you can create a temporary file like so: OUTPUT=$(mktemp). It will create a unique file (sth. like /tmp/tmp.RyR3UlsV5c). However, you will still need to delete it manually, like you already do. mktemp creates such a file and returns (prints) its filename.
    – PerlDuck
    Aug 31 at 12:55












up vote
4
down vote

favorite









up vote
4
down vote

favorite











I have a bash file:



#!/bin/bash

# yesnobox.sh - An inputbox demon shell script
OUTPUT="/tmp/input.txt"

# create empty file
>$OUTPUT

# cleanup - add a trap that will remove $OUTPUT
# if any of the signals - SIGHUP SIGINT SIGTERM it received.
trap "rm $OUTPUT; exit" SIGHUP SIGINT SIGTERM

# show an inputbox
dialog --title "Inputbox"
--backtitle "Search vacancies"
--inputbox "Enter your query " 8 60 2>$OUTPUT

# get respose
respose=$?

# get data stored in $OUPUT using input redirection
name=$(<$OUTPUT)

curl -d '"query":"developer", "turnOff":true' -H "Content-Type: application/json" -X POST http://localhost:8080/explorer


in last string (curl command) I want to set variable name instead "developer". How to correctly insert it?










share|improve this question















I have a bash file:



#!/bin/bash

# yesnobox.sh - An inputbox demon shell script
OUTPUT="/tmp/input.txt"

# create empty file
>$OUTPUT

# cleanup - add a trap that will remove $OUTPUT
# if any of the signals - SIGHUP SIGINT SIGTERM it received.
trap "rm $OUTPUT; exit" SIGHUP SIGINT SIGTERM

# show an inputbox
dialog --title "Inputbox"
--backtitle "Search vacancies"
--inputbox "Enter your query " 8 60 2>$OUTPUT

# get respose
respose=$?

# get data stored in $OUPUT using input redirection
name=$(<$OUTPUT)

curl -d '"query":"developer", "turnOff":true' -H "Content-Type: application/json" -X POST http://localhost:8080/explorer


in last string (curl command) I want to set variable name instead "developer". How to correctly insert it?







command-line scripts curl






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 31 at 15:12









αғsнιη

23.5k2193152




23.5k2193152










asked Aug 31 at 12:40









Валерій Грузицький

585




585







  • 1




    Sidenote: you can create a temporary file like so: OUTPUT=$(mktemp). It will create a unique file (sth. like /tmp/tmp.RyR3UlsV5c). However, you will still need to delete it manually, like you already do. mktemp creates such a file and returns (prints) its filename.
    – PerlDuck
    Aug 31 at 12:55












  • 1




    Sidenote: you can create a temporary file like so: OUTPUT=$(mktemp). It will create a unique file (sth. like /tmp/tmp.RyR3UlsV5c). However, you will still need to delete it manually, like you already do. mktemp creates such a file and returns (prints) its filename.
    – PerlDuck
    Aug 31 at 12:55







1




1




Sidenote: you can create a temporary file like so: OUTPUT=$(mktemp). It will create a unique file (sth. like /tmp/tmp.RyR3UlsV5c). However, you will still need to delete it manually, like you already do. mktemp creates such a file and returns (prints) its filename.
– PerlDuck
Aug 31 at 12:55




Sidenote: you can create a temporary file like so: OUTPUT=$(mktemp). It will create a unique file (sth. like /tmp/tmp.RyR3UlsV5c). However, you will still need to delete it manually, like you already do. mktemp creates such a file and returns (prints) its filename.
– PerlDuck
Aug 31 at 12:55










4 Answers
4






active

oldest

votes

















up vote
10
down vote



accepted










To access variables, you have to put a dollar sign in front of the name: $name



However, variables do not get expanded inside strings enclosed in 'single quotes'. You should have them wrapped inside "double quotes" though, to prevent word splitting of the expanded value, if it might contain spaces.



So there are basically two ways, we either put the whole argument in double quotes to make the variable expandable, but then we have to escape the double quote characters inside, so that they end up in the actual parameter (command line shortened):





curl -d ""query":"$name", "turnOff":true" ...


Alternatively, we can concatenate string literals enclosed in different quote types by writing them immediately next to each other:



curl -d '"query":"'"$name"'", "turnOff":true' ...





share|improve this answer



























    up vote
    6
    down vote













    Since the value for curls -d parameter is within single quotes means that there will be no parameter expansion, just adding the variable would not work. You can get around this by ending the string literal, adding the variable and then starting the string literal again:



    curl -d '"query":"'"$name"'", "turnOff":true' -H "Content-Type: application/json" -X POST http://localhost:8080/explorer


    The extra double quotes around the variable are used to prevent unwanted shell parameter expansion.






    share|improve this answer



























      up vote
      5
      down vote













      @ByteCommander's answer is good, assuming you know that the value of name is a properly escaped JSON string literal. If you can't (or don't want to) make that assumption, use a tool like jq to generate the JSON for you.



      curl -d "$(jq -n --arg n "$name" 'query: $n, turnOff: true')" 
      -H "Content-Type: application/json" -X POST http://localhost:8080/explorer





      share|improve this answer



























        up vote
        2
        down vote













        Some might find this more readable and maintainable since it avoids using escaping, and sequences of single and double quotes, which are hard to follow and match.



        Use Bash's equivalent of sprintf to template the substitution:



        printf -v data '"query":"%s", "turnOff":true' "developer"

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





        share|improve this answer






















          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "89"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1070864%2fhow-to-set-variable-in-the-curl-command-in-bash%23new-answer', 'question_page');

          );

          Post as a guest






























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          10
          down vote



          accepted










          To access variables, you have to put a dollar sign in front of the name: $name



          However, variables do not get expanded inside strings enclosed in 'single quotes'. You should have them wrapped inside "double quotes" though, to prevent word splitting of the expanded value, if it might contain spaces.



          So there are basically two ways, we either put the whole argument in double quotes to make the variable expandable, but then we have to escape the double quote characters inside, so that they end up in the actual parameter (command line shortened):





          curl -d ""query":"$name", "turnOff":true" ...


          Alternatively, we can concatenate string literals enclosed in different quote types by writing them immediately next to each other:



          curl -d '"query":"'"$name"'", "turnOff":true' ...





          share|improve this answer
























            up vote
            10
            down vote



            accepted










            To access variables, you have to put a dollar sign in front of the name: $name



            However, variables do not get expanded inside strings enclosed in 'single quotes'. You should have them wrapped inside "double quotes" though, to prevent word splitting of the expanded value, if it might contain spaces.



            So there are basically two ways, we either put the whole argument in double quotes to make the variable expandable, but then we have to escape the double quote characters inside, so that they end up in the actual parameter (command line shortened):





            curl -d ""query":"$name", "turnOff":true" ...


            Alternatively, we can concatenate string literals enclosed in different quote types by writing them immediately next to each other:



            curl -d '"query":"'"$name"'", "turnOff":true' ...





            share|improve this answer






















              up vote
              10
              down vote



              accepted







              up vote
              10
              down vote



              accepted






              To access variables, you have to put a dollar sign in front of the name: $name



              However, variables do not get expanded inside strings enclosed in 'single quotes'. You should have them wrapped inside "double quotes" though, to prevent word splitting of the expanded value, if it might contain spaces.



              So there are basically two ways, we either put the whole argument in double quotes to make the variable expandable, but then we have to escape the double quote characters inside, so that they end up in the actual parameter (command line shortened):





              curl -d ""query":"$name", "turnOff":true" ...


              Alternatively, we can concatenate string literals enclosed in different quote types by writing them immediately next to each other:



              curl -d '"query":"'"$name"'", "turnOff":true' ...





              share|improve this answer












              To access variables, you have to put a dollar sign in front of the name: $name



              However, variables do not get expanded inside strings enclosed in 'single quotes'. You should have them wrapped inside "double quotes" though, to prevent word splitting of the expanded value, if it might contain spaces.



              So there are basically two ways, we either put the whole argument in double quotes to make the variable expandable, but then we have to escape the double quote characters inside, so that they end up in the actual parameter (command line shortened):





              curl -d ""query":"$name", "turnOff":true" ...


              Alternatively, we can concatenate string literals enclosed in different quote types by writing them immediately next to each other:



              curl -d '"query":"'"$name"'", "turnOff":true' ...






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Aug 31 at 12:52









              Byte Commander

              59.7k26159268




              59.7k26159268






















                  up vote
                  6
                  down vote













                  Since the value for curls -d parameter is within single quotes means that there will be no parameter expansion, just adding the variable would not work. You can get around this by ending the string literal, adding the variable and then starting the string literal again:



                  curl -d '"query":"'"$name"'", "turnOff":true' -H "Content-Type: application/json" -X POST http://localhost:8080/explorer


                  The extra double quotes around the variable are used to prevent unwanted shell parameter expansion.






                  share|improve this answer
























                    up vote
                    6
                    down vote













                    Since the value for curls -d parameter is within single quotes means that there will be no parameter expansion, just adding the variable would not work. You can get around this by ending the string literal, adding the variable and then starting the string literal again:



                    curl -d '"query":"'"$name"'", "turnOff":true' -H "Content-Type: application/json" -X POST http://localhost:8080/explorer


                    The extra double quotes around the variable are used to prevent unwanted shell parameter expansion.






                    share|improve this answer






















                      up vote
                      6
                      down vote










                      up vote
                      6
                      down vote









                      Since the value for curls -d parameter is within single quotes means that there will be no parameter expansion, just adding the variable would not work. You can get around this by ending the string literal, adding the variable and then starting the string literal again:



                      curl -d '"query":"'"$name"'", "turnOff":true' -H "Content-Type: application/json" -X POST http://localhost:8080/explorer


                      The extra double quotes around the variable are used to prevent unwanted shell parameter expansion.






                      share|improve this answer












                      Since the value for curls -d parameter is within single quotes means that there will be no parameter expansion, just adding the variable would not work. You can get around this by ending the string literal, adding the variable and then starting the string literal again:



                      curl -d '"query":"'"$name"'", "turnOff":true' -H "Content-Type: application/json" -X POST http://localhost:8080/explorer


                      The extra double quotes around the variable are used to prevent unwanted shell parameter expansion.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Aug 31 at 12:51









                      mgor

                      851410




                      851410




















                          up vote
                          5
                          down vote













                          @ByteCommander's answer is good, assuming you know that the value of name is a properly escaped JSON string literal. If you can't (or don't want to) make that assumption, use a tool like jq to generate the JSON for you.



                          curl -d "$(jq -n --arg n "$name" 'query: $n, turnOff: true')" 
                          -H "Content-Type: application/json" -X POST http://localhost:8080/explorer





                          share|improve this answer
























                            up vote
                            5
                            down vote













                            @ByteCommander's answer is good, assuming you know that the value of name is a properly escaped JSON string literal. If you can't (or don't want to) make that assumption, use a tool like jq to generate the JSON for you.



                            curl -d "$(jq -n --arg n "$name" 'query: $n, turnOff: true')" 
                            -H "Content-Type: application/json" -X POST http://localhost:8080/explorer





                            share|improve this answer






















                              up vote
                              5
                              down vote










                              up vote
                              5
                              down vote









                              @ByteCommander's answer is good, assuming you know that the value of name is a properly escaped JSON string literal. If you can't (or don't want to) make that assumption, use a tool like jq to generate the JSON for you.



                              curl -d "$(jq -n --arg n "$name" 'query: $n, turnOff: true')" 
                              -H "Content-Type: application/json" -X POST http://localhost:8080/explorer





                              share|improve this answer












                              @ByteCommander's answer is good, assuming you know that the value of name is a properly escaped JSON string literal. If you can't (or don't want to) make that assumption, use a tool like jq to generate the JSON for you.



                              curl -d "$(jq -n --arg n "$name" 'query: $n, turnOff: true')" 
                              -H "Content-Type: application/json" -X POST http://localhost:8080/explorer






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Aug 31 at 14:53









                              chepner

                              1513




                              1513




















                                  up vote
                                  2
                                  down vote













                                  Some might find this more readable and maintainable since it avoids using escaping, and sequences of single and double quotes, which are hard to follow and match.



                                  Use Bash's equivalent of sprintf to template the substitution:



                                  printf -v data '"query":"%s", "turnOff":true' "developer"

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





                                  share|improve this answer


























                                    up vote
                                    2
                                    down vote













                                    Some might find this more readable and maintainable since it avoids using escaping, and sequences of single and double quotes, which are hard to follow and match.



                                    Use Bash's equivalent of sprintf to template the substitution:



                                    printf -v data '"query":"%s", "turnOff":true' "developer"

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





                                    share|improve this answer
























                                      up vote
                                      2
                                      down vote










                                      up vote
                                      2
                                      down vote









                                      Some might find this more readable and maintainable since it avoids using escaping, and sequences of single and double quotes, which are hard to follow and match.



                                      Use Bash's equivalent of sprintf to template the substitution:



                                      printf -v data '"query":"%s", "turnOff":true' "developer"

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





                                      share|improve this answer














                                      Some might find this more readable and maintainable since it avoids using escaping, and sequences of single and double quotes, which are hard to follow and match.



                                      Use Bash's equivalent of sprintf to template the substitution:



                                      printf -v data '"query":"%s", "turnOff":true' "developer"

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






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Aug 31 at 21:02









                                      Sergiy Kolodyazhnyy

                                      65.7k9134287




                                      65.7k9134287










                                      answered Aug 31 at 20:52









                                      Dennis Williamson

                                      1,77221119




                                      1,77221119



























                                           

                                          draft saved


                                          draft discarded















































                                           


                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1070864%2fhow-to-set-variable-in-the-curl-command-in-bash%23new-answer', 'question_page');

                                          );

                                          Post as a guest













































































                                          Popular posts from this blog

                                          How to check contact read email or not when send email to Individual?

                                          Bahrain

                                          Postfix configuration issue with fips on centos 7; mailgun relay