Bash script string interpolation leaves curly braces intact

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











up vote
2
down vote

favorite












The request string in the example below interpolates version variable, but keeps the curly braces and I can't figure out why.



#!/bin/sh

version=2989
request="http://example.com/?version=$version&therest"
echo "$request"


Result:



$ ~/script.sh
http://example.com/?version=2989&therest


Environment:



$ echo $0
-zsh






share|improve this question




















  • You don't run bash in your example, you run sh.
    – user1934428
    Dec 18 '17 at 11:12










  • You have the parenthesis inside a quoted string, so they are output literally. Only $version is replaced, because variable expansion is one the things which does get performed within double-quoted strings.
    – user1934428
    Dec 18 '17 at 11:14














up vote
2
down vote

favorite












The request string in the example below interpolates version variable, but keeps the curly braces and I can't figure out why.



#!/bin/sh

version=2989
request="http://example.com/?version=$version&therest"
echo "$request"


Result:



$ ~/script.sh
http://example.com/?version=2989&therest


Environment:



$ echo $0
-zsh






share|improve this question




















  • You don't run bash in your example, you run sh.
    – user1934428
    Dec 18 '17 at 11:12










  • You have the parenthesis inside a quoted string, so they are output literally. Only $version is replaced, because variable expansion is one the things which does get performed within double-quoted strings.
    – user1934428
    Dec 18 '17 at 11:14












up vote
2
down vote

favorite









up vote
2
down vote

favorite











The request string in the example below interpolates version variable, but keeps the curly braces and I can't figure out why.



#!/bin/sh

version=2989
request="http://example.com/?version=$version&therest"
echo "$request"


Result:



$ ~/script.sh
http://example.com/?version=2989&therest


Environment:



$ echo $0
-zsh






share|improve this question












The request string in the example below interpolates version variable, but keeps the curly braces and I can't figure out why.



#!/bin/sh

version=2989
request="http://example.com/?version=$version&therest"
echo "$request"


Result:



$ ~/script.sh
http://example.com/?version=2989&therest


Environment:



$ echo $0
-zsh








share|improve this question











share|improve this question




share|improve this question










asked Dec 15 '17 at 20:02









Alexei Danchenkov

207311




207311











  • You don't run bash in your example, you run sh.
    – user1934428
    Dec 18 '17 at 11:12










  • You have the parenthesis inside a quoted string, so they are output literally. Only $version is replaced, because variable expansion is one the things which does get performed within double-quoted strings.
    – user1934428
    Dec 18 '17 at 11:14
















  • You don't run bash in your example, you run sh.
    – user1934428
    Dec 18 '17 at 11:12










  • You have the parenthesis inside a quoted string, so they are output literally. Only $version is replaced, because variable expansion is one the things which does get performed within double-quoted strings.
    – user1934428
    Dec 18 '17 at 11:14















You don't run bash in your example, you run sh.
– user1934428
Dec 18 '17 at 11:12




You don't run bash in your example, you run sh.
– user1934428
Dec 18 '17 at 11:12












You have the parenthesis inside a quoted string, so they are output literally. Only $version is replaced, because variable expansion is one the things which does get performed within double-quoted strings.
– user1934428
Dec 18 '17 at 11:14




You have the parenthesis inside a quoted string, so they are output literally. Only $version is replaced, because variable expansion is one the things which does get performed within double-quoted strings.
– user1934428
Dec 18 '17 at 11:14










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










The { is before $. It should be $version :)






share|improve this answer



























    up vote
    1
    down vote













    Compare:



    $ version=1.2; echo "http://example.com/?version=$version&therest"
    http://example.com/?version=1.2&therest
    $ version=1.2; echo "http://example.com/?version=$version&therest"
    http://example.com/?version=1.2&therest


    Inside quoted, braces are regarded as just normal characters unless they are part of some construct like $variableName.






    share|improve this answer




















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      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: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      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%2funix.stackexchange.com%2fquestions%2f411122%2fbash-script-string-interpolation-leaves-curly-braces-intact%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote



      accepted










      The { is before $. It should be $version :)






      share|improve this answer
























        up vote
        3
        down vote



        accepted










        The { is before $. It should be $version :)






        share|improve this answer






















          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          The { is before $. It should be $version :)






          share|improve this answer












          The { is before $. It should be $version :)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 15 '17 at 20:07









          m0dular

          63115




          63115






















              up vote
              1
              down vote













              Compare:



              $ version=1.2; echo "http://example.com/?version=$version&therest"
              http://example.com/?version=1.2&therest
              $ version=1.2; echo "http://example.com/?version=$version&therest"
              http://example.com/?version=1.2&therest


              Inside quoted, braces are regarded as just normal characters unless they are part of some construct like $variableName.






              share|improve this answer
























                up vote
                1
                down vote













                Compare:



                $ version=1.2; echo "http://example.com/?version=$version&therest"
                http://example.com/?version=1.2&therest
                $ version=1.2; echo "http://example.com/?version=$version&therest"
                http://example.com/?version=1.2&therest


                Inside quoted, braces are regarded as just normal characters unless they are part of some construct like $variableName.






                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  Compare:



                  $ version=1.2; echo "http://example.com/?version=$version&therest"
                  http://example.com/?version=1.2&therest
                  $ version=1.2; echo "http://example.com/?version=$version&therest"
                  http://example.com/?version=1.2&therest


                  Inside quoted, braces are regarded as just normal characters unless they are part of some construct like $variableName.






                  share|improve this answer












                  Compare:



                  $ version=1.2; echo "http://example.com/?version=$version&therest"
                  http://example.com/?version=1.2&therest
                  $ version=1.2; echo "http://example.com/?version=$version&therest"
                  http://example.com/?version=1.2&therest


                  Inside quoted, braces are regarded as just normal characters unless they are part of some construct like $variableName.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 15 '17 at 20:07









                  John1024

                  44.2k4100116




                  44.2k4100116






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f411122%2fbash-script-string-interpolation-leaves-curly-braces-intact%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Peggy Mitchell

                      Palaiologos

                      The Forum (Inglewood, California)