How to include environment variable in bash line CURL?











up vote
28
down vote

favorite
9












Trying to get Transmission to notify when download complete.



This works:



curl -u <my-api-token>: 
-X POST https://api.pushbullet.com/v2/pushes
--header 'Content-Type: application/json'
--data-binary '{"type": "note", "title": "$TR_TORRENT_NAME",
"body": "$TR_TORRENT_NAME completed."}'


... except it pushes $TR_TORRENT_NAME and not the actual contents of that variable.



Do I need to escape some quote or something?










share|improve this question




























    up vote
    28
    down vote

    favorite
    9












    Trying to get Transmission to notify when download complete.



    This works:



    curl -u <my-api-token>: 
    -X POST https://api.pushbullet.com/v2/pushes
    --header 'Content-Type: application/json'
    --data-binary '{"type": "note", "title": "$TR_TORRENT_NAME",
    "body": "$TR_TORRENT_NAME completed."}'


    ... except it pushes $TR_TORRENT_NAME and not the actual contents of that variable.



    Do I need to escape some quote or something?










    share|improve this question


























      up vote
      28
      down vote

      favorite
      9









      up vote
      28
      down vote

      favorite
      9






      9





      Trying to get Transmission to notify when download complete.



      This works:



      curl -u <my-api-token>: 
      -X POST https://api.pushbullet.com/v2/pushes
      --header 'Content-Type: application/json'
      --data-binary '{"type": "note", "title": "$TR_TORRENT_NAME",
      "body": "$TR_TORRENT_NAME completed."}'


      ... except it pushes $TR_TORRENT_NAME and not the actual contents of that variable.



      Do I need to escape some quote or something?










      share|improve this question















      Trying to get Transmission to notify when download complete.



      This works:



      curl -u <my-api-token>: 
      -X POST https://api.pushbullet.com/v2/pushes
      --header 'Content-Type: application/json'
      --data-binary '{"type": "note", "title": "$TR_TORRENT_NAME",
      "body": "$TR_TORRENT_NAME completed."}'


      ... except it pushes $TR_TORRENT_NAME and not the actual contents of that variable.



      Do I need to escape some quote or something?







      bash environment-variables






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 4 '14 at 12:57









      Raúl Salinas-Monteagudo

      940410




      940410










      asked Nov 4 '14 at 2:44









      Ze'ev

      84131538




      84131538






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          43
          down vote



          accepted










          Inside single-quotes, the shell expands nothing. Place them inside double-quotes instead:



          curl -u <my-api-token>: 
          -X POST https://api.pushbullet.com/v2/pushes
          --header 'Content-Type: application/json'
          --data-binary '{"type": "note", "title": "'"$TR_TORRENT_NAME"'",
          "body": "'"$TR_TORRENT_NAME completed"'."}'


          Let's examine how this works by looking at:



          $ TR_TORRENT_NAME=MyTorrent
          $ echo '{"type": "note", "title": "'"$TR_TORRENT_NAME"'", "body": "'"$TR_TORRENT_NAME completed"'."}'
          {"type": "note", "title": "MyTorrent", "body": "MyTorrent completed."}


          When the shell variable appears, it is always inside double-quotes. Consequently, it is properly expanded.



          Quoting like this is a bit subtle. We have single-quoted strings that contain double-quotes as characters and are next to double-quoted strings. To understand this better, let's take this fragment as a an example:



           "'"$TR_TORRENT_NAME"'"


          Taking each character in turn:




          1. " is a literal double-quote character that is inside of a single-quoted string. (For brevity, the beginning of this string is not shown in this fragment.)


          2. ' closes a single-quoted string.


          3. " opens a double-quoted string.


          4. $TR_TORRENT_NAME is a shell variable that is expanded inside double-quotes.


          5. " closes the double-quoted string.


          6. ' opens a new single-quoted string.


          7. " places a double-quote character inside the single-quoted string.







          share|improve this answer























          • Do you need steps 3 and 5?
            – davidfrancis
            Jan 18 at 11:39










          • @davidfrancis If one omits steps 3 and 5, then step 4 is subject to word splitting and pathname expansion and either one could cause all manor of trouble. Unless one explicitly wants word splitting and pathname expansion, a shell variable should always be inside double-quotes.
            – John1024
            Jan 18 at 20:49












          • Thanks for that, can you give a quick example please? It worked in my own example, which is why I asked, but there were no spaces or anything else complex in there
            – davidfrancis
            Jan 19 at 9:10










          • @davidfrancis Try TR_TORRENT_NAME="A * B" and see what happens.
            – John1024
            Jan 20 at 8:17


















          up vote
          1
          down vote













          To include an environment variable in a bash line curl without quotes around the variable content, this worked for me:



          --header 'PRIVATE-TOKEN: '"$PRIVATE_TOKEN"''


          Or using the scenario that was first described without quotes around the body field:



          curl -u <my-api-token>: 
          -X POST https://api.pushbullet.com/v2/pushes
          --header 'Content-Type: application/json'
          --data-binary '{"type": "note", "title": "'"$TR_TORRENT_NAME"'",
          "body": '"$TR_TORRENT_NAME completed"'.}'





          share|improve this answer





















            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "3"
            };
            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: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f835587%2fhow-to-include-environment-variable-in-bash-line-curl%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            43
            down vote



            accepted










            Inside single-quotes, the shell expands nothing. Place them inside double-quotes instead:



            curl -u <my-api-token>: 
            -X POST https://api.pushbullet.com/v2/pushes
            --header 'Content-Type: application/json'
            --data-binary '{"type": "note", "title": "'"$TR_TORRENT_NAME"'",
            "body": "'"$TR_TORRENT_NAME completed"'."}'


            Let's examine how this works by looking at:



            $ TR_TORRENT_NAME=MyTorrent
            $ echo '{"type": "note", "title": "'"$TR_TORRENT_NAME"'", "body": "'"$TR_TORRENT_NAME completed"'."}'
            {"type": "note", "title": "MyTorrent", "body": "MyTorrent completed."}


            When the shell variable appears, it is always inside double-quotes. Consequently, it is properly expanded.



            Quoting like this is a bit subtle. We have single-quoted strings that contain double-quotes as characters and are next to double-quoted strings. To understand this better, let's take this fragment as a an example:



             "'"$TR_TORRENT_NAME"'"


            Taking each character in turn:




            1. " is a literal double-quote character that is inside of a single-quoted string. (For brevity, the beginning of this string is not shown in this fragment.)


            2. ' closes a single-quoted string.


            3. " opens a double-quoted string.


            4. $TR_TORRENT_NAME is a shell variable that is expanded inside double-quotes.


            5. " closes the double-quoted string.


            6. ' opens a new single-quoted string.


            7. " places a double-quote character inside the single-quoted string.







            share|improve this answer























            • Do you need steps 3 and 5?
              – davidfrancis
              Jan 18 at 11:39










            • @davidfrancis If one omits steps 3 and 5, then step 4 is subject to word splitting and pathname expansion and either one could cause all manor of trouble. Unless one explicitly wants word splitting and pathname expansion, a shell variable should always be inside double-quotes.
              – John1024
              Jan 18 at 20:49












            • Thanks for that, can you give a quick example please? It worked in my own example, which is why I asked, but there were no spaces or anything else complex in there
              – davidfrancis
              Jan 19 at 9:10










            • @davidfrancis Try TR_TORRENT_NAME="A * B" and see what happens.
              – John1024
              Jan 20 at 8:17















            up vote
            43
            down vote



            accepted










            Inside single-quotes, the shell expands nothing. Place them inside double-quotes instead:



            curl -u <my-api-token>: 
            -X POST https://api.pushbullet.com/v2/pushes
            --header 'Content-Type: application/json'
            --data-binary '{"type": "note", "title": "'"$TR_TORRENT_NAME"'",
            "body": "'"$TR_TORRENT_NAME completed"'."}'


            Let's examine how this works by looking at:



            $ TR_TORRENT_NAME=MyTorrent
            $ echo '{"type": "note", "title": "'"$TR_TORRENT_NAME"'", "body": "'"$TR_TORRENT_NAME completed"'."}'
            {"type": "note", "title": "MyTorrent", "body": "MyTorrent completed."}


            When the shell variable appears, it is always inside double-quotes. Consequently, it is properly expanded.



            Quoting like this is a bit subtle. We have single-quoted strings that contain double-quotes as characters and are next to double-quoted strings. To understand this better, let's take this fragment as a an example:



             "'"$TR_TORRENT_NAME"'"


            Taking each character in turn:




            1. " is a literal double-quote character that is inside of a single-quoted string. (For brevity, the beginning of this string is not shown in this fragment.)


            2. ' closes a single-quoted string.


            3. " opens a double-quoted string.


            4. $TR_TORRENT_NAME is a shell variable that is expanded inside double-quotes.


            5. " closes the double-quoted string.


            6. ' opens a new single-quoted string.


            7. " places a double-quote character inside the single-quoted string.







            share|improve this answer























            • Do you need steps 3 and 5?
              – davidfrancis
              Jan 18 at 11:39










            • @davidfrancis If one omits steps 3 and 5, then step 4 is subject to word splitting and pathname expansion and either one could cause all manor of trouble. Unless one explicitly wants word splitting and pathname expansion, a shell variable should always be inside double-quotes.
              – John1024
              Jan 18 at 20:49












            • Thanks for that, can you give a quick example please? It worked in my own example, which is why I asked, but there were no spaces or anything else complex in there
              – davidfrancis
              Jan 19 at 9:10










            • @davidfrancis Try TR_TORRENT_NAME="A * B" and see what happens.
              – John1024
              Jan 20 at 8:17













            up vote
            43
            down vote



            accepted







            up vote
            43
            down vote



            accepted






            Inside single-quotes, the shell expands nothing. Place them inside double-quotes instead:



            curl -u <my-api-token>: 
            -X POST https://api.pushbullet.com/v2/pushes
            --header 'Content-Type: application/json'
            --data-binary '{"type": "note", "title": "'"$TR_TORRENT_NAME"'",
            "body": "'"$TR_TORRENT_NAME completed"'."}'


            Let's examine how this works by looking at:



            $ TR_TORRENT_NAME=MyTorrent
            $ echo '{"type": "note", "title": "'"$TR_TORRENT_NAME"'", "body": "'"$TR_TORRENT_NAME completed"'."}'
            {"type": "note", "title": "MyTorrent", "body": "MyTorrent completed."}


            When the shell variable appears, it is always inside double-quotes. Consequently, it is properly expanded.



            Quoting like this is a bit subtle. We have single-quoted strings that contain double-quotes as characters and are next to double-quoted strings. To understand this better, let's take this fragment as a an example:



             "'"$TR_TORRENT_NAME"'"


            Taking each character in turn:




            1. " is a literal double-quote character that is inside of a single-quoted string. (For brevity, the beginning of this string is not shown in this fragment.)


            2. ' closes a single-quoted string.


            3. " opens a double-quoted string.


            4. $TR_TORRENT_NAME is a shell variable that is expanded inside double-quotes.


            5. " closes the double-quoted string.


            6. ' opens a new single-quoted string.


            7. " places a double-quote character inside the single-quoted string.







            share|improve this answer














            Inside single-quotes, the shell expands nothing. Place them inside double-quotes instead:



            curl -u <my-api-token>: 
            -X POST https://api.pushbullet.com/v2/pushes
            --header 'Content-Type: application/json'
            --data-binary '{"type": "note", "title": "'"$TR_TORRENT_NAME"'",
            "body": "'"$TR_TORRENT_NAME completed"'."}'


            Let's examine how this works by looking at:



            $ TR_TORRENT_NAME=MyTorrent
            $ echo '{"type": "note", "title": "'"$TR_TORRENT_NAME"'", "body": "'"$TR_TORRENT_NAME completed"'."}'
            {"type": "note", "title": "MyTorrent", "body": "MyTorrent completed."}


            When the shell variable appears, it is always inside double-quotes. Consequently, it is properly expanded.



            Quoting like this is a bit subtle. We have single-quoted strings that contain double-quotes as characters and are next to double-quoted strings. To understand this better, let's take this fragment as a an example:



             "'"$TR_TORRENT_NAME"'"


            Taking each character in turn:




            1. " is a literal double-quote character that is inside of a single-quoted string. (For brevity, the beginning of this string is not shown in this fragment.)


            2. ' closes a single-quoted string.


            3. " opens a double-quoted string.


            4. $TR_TORRENT_NAME is a shell variable that is expanded inside double-quotes.


            5. " closes the double-quoted string.


            6. ' opens a new single-quoted string.


            7. " places a double-quote character inside the single-quoted string.








            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 3 '16 at 6:35

























            answered Nov 4 '14 at 2:50









            John1024

            12.6k43433




            12.6k43433












            • Do you need steps 3 and 5?
              – davidfrancis
              Jan 18 at 11:39










            • @davidfrancis If one omits steps 3 and 5, then step 4 is subject to word splitting and pathname expansion and either one could cause all manor of trouble. Unless one explicitly wants word splitting and pathname expansion, a shell variable should always be inside double-quotes.
              – John1024
              Jan 18 at 20:49












            • Thanks for that, can you give a quick example please? It worked in my own example, which is why I asked, but there were no spaces or anything else complex in there
              – davidfrancis
              Jan 19 at 9:10










            • @davidfrancis Try TR_TORRENT_NAME="A * B" and see what happens.
              – John1024
              Jan 20 at 8:17


















            • Do you need steps 3 and 5?
              – davidfrancis
              Jan 18 at 11:39










            • @davidfrancis If one omits steps 3 and 5, then step 4 is subject to word splitting and pathname expansion and either one could cause all manor of trouble. Unless one explicitly wants word splitting and pathname expansion, a shell variable should always be inside double-quotes.
              – John1024
              Jan 18 at 20:49












            • Thanks for that, can you give a quick example please? It worked in my own example, which is why I asked, but there were no spaces or anything else complex in there
              – davidfrancis
              Jan 19 at 9:10










            • @davidfrancis Try TR_TORRENT_NAME="A * B" and see what happens.
              – John1024
              Jan 20 at 8:17
















            Do you need steps 3 and 5?
            – davidfrancis
            Jan 18 at 11:39




            Do you need steps 3 and 5?
            – davidfrancis
            Jan 18 at 11:39












            @davidfrancis If one omits steps 3 and 5, then step 4 is subject to word splitting and pathname expansion and either one could cause all manor of trouble. Unless one explicitly wants word splitting and pathname expansion, a shell variable should always be inside double-quotes.
            – John1024
            Jan 18 at 20:49






            @davidfrancis If one omits steps 3 and 5, then step 4 is subject to word splitting and pathname expansion and either one could cause all manor of trouble. Unless one explicitly wants word splitting and pathname expansion, a shell variable should always be inside double-quotes.
            – John1024
            Jan 18 at 20:49














            Thanks for that, can you give a quick example please? It worked in my own example, which is why I asked, but there were no spaces or anything else complex in there
            – davidfrancis
            Jan 19 at 9:10




            Thanks for that, can you give a quick example please? It worked in my own example, which is why I asked, but there were no spaces or anything else complex in there
            – davidfrancis
            Jan 19 at 9:10












            @davidfrancis Try TR_TORRENT_NAME="A * B" and see what happens.
            – John1024
            Jan 20 at 8:17




            @davidfrancis Try TR_TORRENT_NAME="A * B" and see what happens.
            – John1024
            Jan 20 at 8:17












            up vote
            1
            down vote













            To include an environment variable in a bash line curl without quotes around the variable content, this worked for me:



            --header 'PRIVATE-TOKEN: '"$PRIVATE_TOKEN"''


            Or using the scenario that was first described without quotes around the body field:



            curl -u <my-api-token>: 
            -X POST https://api.pushbullet.com/v2/pushes
            --header 'Content-Type: application/json'
            --data-binary '{"type": "note", "title": "'"$TR_TORRENT_NAME"'",
            "body": '"$TR_TORRENT_NAME completed"'.}'





            share|improve this answer

























              up vote
              1
              down vote













              To include an environment variable in a bash line curl without quotes around the variable content, this worked for me:



              --header 'PRIVATE-TOKEN: '"$PRIVATE_TOKEN"''


              Or using the scenario that was first described without quotes around the body field:



              curl -u <my-api-token>: 
              -X POST https://api.pushbullet.com/v2/pushes
              --header 'Content-Type: application/json'
              --data-binary '{"type": "note", "title": "'"$TR_TORRENT_NAME"'",
              "body": '"$TR_TORRENT_NAME completed"'.}'





              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                To include an environment variable in a bash line curl without quotes around the variable content, this worked for me:



                --header 'PRIVATE-TOKEN: '"$PRIVATE_TOKEN"''


                Or using the scenario that was first described without quotes around the body field:



                curl -u <my-api-token>: 
                -X POST https://api.pushbullet.com/v2/pushes
                --header 'Content-Type: application/json'
                --data-binary '{"type": "note", "title": "'"$TR_TORRENT_NAME"'",
                "body": '"$TR_TORRENT_NAME completed"'.}'





                share|improve this answer












                To include an environment variable in a bash line curl without quotes around the variable content, this worked for me:



                --header 'PRIVATE-TOKEN: '"$PRIVATE_TOKEN"''


                Or using the scenario that was first described without quotes around the body field:



                curl -u <my-api-token>: 
                -X POST https://api.pushbullet.com/v2/pushes
                --header 'Content-Type: application/json'
                --data-binary '{"type": "note", "title": "'"$TR_TORRENT_NAME"'",
                "body": '"$TR_TORRENT_NAME completed"'.}'






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Sep 26 at 17:58









                Brad Natelborg

                112




                112






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Super User!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f835587%2fhow-to-include-environment-variable-in-bash-line-curl%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Plaza Victoria

                    In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

                    How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...