Escaping ampersand (&) in URL in Bash under WSL

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











up vote
0
down vote

favorite












I'm trying to write a Bash function which will take some arguments from the command line and put them in a URL which contains URL parameters (i.e. contains ? and &).



If there is only one parameter in the URL, there is no &, and there is no problem, i.e., if I define the following function:



test() 
cmd.exe /c start https://example.com/?foo=$1



and invoke it with test bar, it opens the URL https://example.com/?foo=bar in my browser, which is completely correct.



The problem is when I want to add a second URL parameter. I then extend the function as follows:



test() 
cmd.exe /c start https://example.com/?foo=$1&baz=$2



but when I invoke it with test bar qux, the same URL as before (https://example.com/?foo=bar) is opened in my browser, and my terminal displays the error 'baz' is not recognized as an internal or external command, operable program or batch file.



Wrapping the URL in double quotes also does not help: when I change it to



test() 
cmd.exe /c start "https://example.com/?foo=$1&baz=$2"



it opens the URL https://example.com//?foo=bar", and I still get the error 'baz' is not recognized as an internal or external command, operable program or batch file.







share|improve this question























    up vote
    0
    down vote

    favorite












    I'm trying to write a Bash function which will take some arguments from the command line and put them in a URL which contains URL parameters (i.e. contains ? and &).



    If there is only one parameter in the URL, there is no &, and there is no problem, i.e., if I define the following function:



    test() 
    cmd.exe /c start https://example.com/?foo=$1



    and invoke it with test bar, it opens the URL https://example.com/?foo=bar in my browser, which is completely correct.



    The problem is when I want to add a second URL parameter. I then extend the function as follows:



    test() 
    cmd.exe /c start https://example.com/?foo=$1&baz=$2



    but when I invoke it with test bar qux, the same URL as before (https://example.com/?foo=bar) is opened in my browser, and my terminal displays the error 'baz' is not recognized as an internal or external command, operable program or batch file.



    Wrapping the URL in double quotes also does not help: when I change it to



    test() 
    cmd.exe /c start "https://example.com/?foo=$1&baz=$2"



    it opens the URL https://example.com//?foo=bar", and I still get the error 'baz' is not recognized as an internal or external command, operable program or batch file.







    share|improve this question





















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm trying to write a Bash function which will take some arguments from the command line and put them in a URL which contains URL parameters (i.e. contains ? and &).



      If there is only one parameter in the URL, there is no &, and there is no problem, i.e., if I define the following function:



      test() 
      cmd.exe /c start https://example.com/?foo=$1



      and invoke it with test bar, it opens the URL https://example.com/?foo=bar in my browser, which is completely correct.



      The problem is when I want to add a second URL parameter. I then extend the function as follows:



      test() 
      cmd.exe /c start https://example.com/?foo=$1&baz=$2



      but when I invoke it with test bar qux, the same URL as before (https://example.com/?foo=bar) is opened in my browser, and my terminal displays the error 'baz' is not recognized as an internal or external command, operable program or batch file.



      Wrapping the URL in double quotes also does not help: when I change it to



      test() 
      cmd.exe /c start "https://example.com/?foo=$1&baz=$2"



      it opens the URL https://example.com//?foo=bar", and I still get the error 'baz' is not recognized as an internal or external command, operable program or batch file.







      share|improve this question











      I'm trying to write a Bash function which will take some arguments from the command line and put them in a URL which contains URL parameters (i.e. contains ? and &).



      If there is only one parameter in the URL, there is no &, and there is no problem, i.e., if I define the following function:



      test() 
      cmd.exe /c start https://example.com/?foo=$1



      and invoke it with test bar, it opens the URL https://example.com/?foo=bar in my browser, which is completely correct.



      The problem is when I want to add a second URL parameter. I then extend the function as follows:



      test() 
      cmd.exe /c start https://example.com/?foo=$1&baz=$2



      but when I invoke it with test bar qux, the same URL as before (https://example.com/?foo=bar) is opened in my browser, and my terminal displays the error 'baz' is not recognized as an internal or external command, operable program or batch file.



      Wrapping the URL in double quotes also does not help: when I change it to



      test() 
      cmd.exe /c start "https://example.com/?foo=$1&baz=$2"



      it opens the URL https://example.com//?foo=bar", and I still get the error 'baz' is not recognized as an internal or external command, operable program or batch file.









      share|improve this question










      share|improve this question




      share|improve this question









      asked Jun 6 at 12:40









      Jamy Mahabier

      212




      212




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Since cmd.exe is invoked with a command, the cmd.exe escape syntax needs to be used, rather than the Bash escape syntax, i.e. & needs to be escaped as ^& (and ? does not need escaping). The following works as intended:



          test() 
          cmd.exe /c start "https://example.com/?foo=$1^&baz=$2"



          (Note that 'baz' is not recognized as an internal or external command, operable program or batch file. is an error generated by cmd.exe: it refers to a batch file, which is a Windows concept.)






          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%2f448195%2fescaping-ampersand-in-url-in-bash-under-wsl%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            Since cmd.exe is invoked with a command, the cmd.exe escape syntax needs to be used, rather than the Bash escape syntax, i.e. & needs to be escaped as ^& (and ? does not need escaping). The following works as intended:



            test() 
            cmd.exe /c start "https://example.com/?foo=$1^&baz=$2"



            (Note that 'baz' is not recognized as an internal or external command, operable program or batch file. is an error generated by cmd.exe: it refers to a batch file, which is a Windows concept.)






            share|improve this answer

























              up vote
              2
              down vote



              accepted










              Since cmd.exe is invoked with a command, the cmd.exe escape syntax needs to be used, rather than the Bash escape syntax, i.e. & needs to be escaped as ^& (and ? does not need escaping). The following works as intended:



              test() 
              cmd.exe /c start "https://example.com/?foo=$1^&baz=$2"



              (Note that 'baz' is not recognized as an internal or external command, operable program or batch file. is an error generated by cmd.exe: it refers to a batch file, which is a Windows concept.)






              share|improve this answer























                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted






                Since cmd.exe is invoked with a command, the cmd.exe escape syntax needs to be used, rather than the Bash escape syntax, i.e. & needs to be escaped as ^& (and ? does not need escaping). The following works as intended:



                test() 
                cmd.exe /c start "https://example.com/?foo=$1^&baz=$2"



                (Note that 'baz' is not recognized as an internal or external command, operable program or batch file. is an error generated by cmd.exe: it refers to a batch file, which is a Windows concept.)






                share|improve this answer













                Since cmd.exe is invoked with a command, the cmd.exe escape syntax needs to be used, rather than the Bash escape syntax, i.e. & needs to be escaped as ^& (and ? does not need escaping). The following works as intended:



                test() 
                cmd.exe /c start "https://example.com/?foo=$1^&baz=$2"



                (Note that 'baz' is not recognized as an internal or external command, operable program or batch file. is an error generated by cmd.exe: it refers to a batch file, which is a Windows concept.)







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Jun 6 at 12:44









                Jamy Mahabier

                212




                212






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f448195%2fescaping-ampersand-in-url-in-bash-under-wsl%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?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?