How do I use command-line arguments to `sh` in the `-c` command string?












3
















I know that



sh -c 'echo $1' sh 4


will output 4.
and



sh -c 'echo $2' sh 4 5


will output 5.



But I cannot understand how the parameters after the second sh were passed to the command next to sh -c. I read the man page of both bash and dash but could not find the introduction about this kind of syntax.










share|improve this question





























    3
















    I know that



    sh -c 'echo $1' sh 4


    will output 4.
    and



    sh -c 'echo $2' sh 4 5


    will output 5.



    But I cannot understand how the parameters after the second sh were passed to the command next to sh -c. I read the man page of both bash and dash but could not find the introduction about this kind of syntax.










    share|improve this question



























      3












      3








      3









      I know that



      sh -c 'echo $1' sh 4


      will output 4.
      and



      sh -c 'echo $2' sh 4 5


      will output 5.



      But I cannot understand how the parameters after the second sh were passed to the command next to sh -c. I read the man page of both bash and dash but could not find the introduction about this kind of syntax.










      share|improve this question

















      I know that



      sh -c 'echo $1' sh 4


      will output 4.
      and



      sh -c 'echo $2' sh 4 5


      will output 5.



      But I cannot understand how the parameters after the second sh were passed to the command next to sh -c. I read the man page of both bash and dash but could not find the introduction about this kind of syntax.







      command-line bash sh dash-shell






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 7 at 7:32









      dessert

      22.2k56198




      22.2k56198










      asked Jan 7 at 6:24









      gbcatgbcat

      185




      185






















          2 Answers
          2






          active

          oldest

          votes


















          4














          From man sh:



          -c string If  the  -c  option  is  present, then commands are read from
          string. If there are arguments after the string, they are
          assigned to the positional parameters, starting with $0.


          In your command the second sh is just a parameter with position 0 while 4 has position 1 and so on.



          You can run this to check:



          $ sh -c 'echo $0' sh 4 5
          sh





          share|improve this answer























          • That seems to be the text in Bash's man page. It's actually incorrect in that it implies that $0 is one of the positional parameters. It isn't. The POSIX definition states that "A positional parameter is a parameter denoted by the decimal value represented by one or more digits, other than the single digit 0.", and Bash's manual itself contains that same definition.
            – ilkkachu
            2 days ago





















          4














          This behavior is actually specified by POSIX standard, which all Bourne-like shells should support to claim themselves portable.




          sh -c [-abCefhimnuvx] [-o option]... [+abCefhimnuvx] [+o option]...
          command_string [command_name [argument...]]




          See the command_string parameter ? Now let's look at -c flag description:




          -c



          Read commands from the command_string operand. Set the value of special parameter 0 (see Special Parameters) from the value of the command_name operand and the positional parameters ($1, $2, and so on) in sequence from the remaining argument operands. No commands shall be read from the standard input.




          In other words, where in normal shell script $0 (which is usually shell name in interactive mode or script name when you run a script) would be set by the shell itself, with -c you have to specify it yourself. Thus,



          sh -c 'echo Hi, I am $0 , my first positional parameter is $1' foobar 5


          would set the process name to sh foobar.



          Just in case you're wondering what $0 is, it's also covered in "Special Parameters" section of Shell Command Language Specifications:




          0



          (Zero.) Expands to the name of the shell or shell script.







          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',
            autoActivateHeartbeat: false,
            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%2faskubuntu.com%2fquestions%2f1107617%2fhow-do-i-use-command-line-arguments-to-sh-in-the-c-command-string%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









            4














            From man sh:



            -c string If  the  -c  option  is  present, then commands are read from
            string. If there are arguments after the string, they are
            assigned to the positional parameters, starting with $0.


            In your command the second sh is just a parameter with position 0 while 4 has position 1 and so on.



            You can run this to check:



            $ sh -c 'echo $0' sh 4 5
            sh





            share|improve this answer























            • That seems to be the text in Bash's man page. It's actually incorrect in that it implies that $0 is one of the positional parameters. It isn't. The POSIX definition states that "A positional parameter is a parameter denoted by the decimal value represented by one or more digits, other than the single digit 0.", and Bash's manual itself contains that same definition.
              – ilkkachu
              2 days ago


















            4














            From man sh:



            -c string If  the  -c  option  is  present, then commands are read from
            string. If there are arguments after the string, they are
            assigned to the positional parameters, starting with $0.


            In your command the second sh is just a parameter with position 0 while 4 has position 1 and so on.



            You can run this to check:



            $ sh -c 'echo $0' sh 4 5
            sh





            share|improve this answer























            • That seems to be the text in Bash's man page. It's actually incorrect in that it implies that $0 is one of the positional parameters. It isn't. The POSIX definition states that "A positional parameter is a parameter denoted by the decimal value represented by one or more digits, other than the single digit 0.", and Bash's manual itself contains that same definition.
              – ilkkachu
              2 days ago
















            4












            4








            4






            From man sh:



            -c string If  the  -c  option  is  present, then commands are read from
            string. If there are arguments after the string, they are
            assigned to the positional parameters, starting with $0.


            In your command the second sh is just a parameter with position 0 while 4 has position 1 and so on.



            You can run this to check:



            $ sh -c 'echo $0' sh 4 5
            sh





            share|improve this answer














            From man sh:



            -c string If  the  -c  option  is  present, then commands are read from
            string. If there are arguments after the string, they are
            assigned to the positional parameters, starting with $0.


            In your command the second sh is just a parameter with position 0 while 4 has position 1 and so on.



            You can run this to check:



            $ sh -c 'echo $0' sh 4 5
            sh






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 2 days ago









            dessert

            22.2k56198




            22.2k56198










            answered Jan 7 at 6:40









            eyadofeyadof

            1,20411517




            1,20411517












            • That seems to be the text in Bash's man page. It's actually incorrect in that it implies that $0 is one of the positional parameters. It isn't. The POSIX definition states that "A positional parameter is a parameter denoted by the decimal value represented by one or more digits, other than the single digit 0.", and Bash's manual itself contains that same definition.
              – ilkkachu
              2 days ago




















            • That seems to be the text in Bash's man page. It's actually incorrect in that it implies that $0 is one of the positional parameters. It isn't. The POSIX definition states that "A positional parameter is a parameter denoted by the decimal value represented by one or more digits, other than the single digit 0.", and Bash's manual itself contains that same definition.
              – ilkkachu
              2 days ago


















            That seems to be the text in Bash's man page. It's actually incorrect in that it implies that $0 is one of the positional parameters. It isn't. The POSIX definition states that "A positional parameter is a parameter denoted by the decimal value represented by one or more digits, other than the single digit 0.", and Bash's manual itself contains that same definition.
            – ilkkachu
            2 days ago






            That seems to be the text in Bash's man page. It's actually incorrect in that it implies that $0 is one of the positional parameters. It isn't. The POSIX definition states that "A positional parameter is a parameter denoted by the decimal value represented by one or more digits, other than the single digit 0.", and Bash's manual itself contains that same definition.
            – ilkkachu
            2 days ago















            4














            This behavior is actually specified by POSIX standard, which all Bourne-like shells should support to claim themselves portable.




            sh -c [-abCefhimnuvx] [-o option]... [+abCefhimnuvx] [+o option]...
            command_string [command_name [argument...]]




            See the command_string parameter ? Now let's look at -c flag description:




            -c



            Read commands from the command_string operand. Set the value of special parameter 0 (see Special Parameters) from the value of the command_name operand and the positional parameters ($1, $2, and so on) in sequence from the remaining argument operands. No commands shall be read from the standard input.




            In other words, where in normal shell script $0 (which is usually shell name in interactive mode or script name when you run a script) would be set by the shell itself, with -c you have to specify it yourself. Thus,



            sh -c 'echo Hi, I am $0 , my first positional parameter is $1' foobar 5


            would set the process name to sh foobar.



            Just in case you're wondering what $0 is, it's also covered in "Special Parameters" section of Shell Command Language Specifications:




            0



            (Zero.) Expands to the name of the shell or shell script.







            share|improve this answer




























              4














              This behavior is actually specified by POSIX standard, which all Bourne-like shells should support to claim themselves portable.




              sh -c [-abCefhimnuvx] [-o option]... [+abCefhimnuvx] [+o option]...
              command_string [command_name [argument...]]




              See the command_string parameter ? Now let's look at -c flag description:




              -c



              Read commands from the command_string operand. Set the value of special parameter 0 (see Special Parameters) from the value of the command_name operand and the positional parameters ($1, $2, and so on) in sequence from the remaining argument operands. No commands shall be read from the standard input.




              In other words, where in normal shell script $0 (which is usually shell name in interactive mode or script name when you run a script) would be set by the shell itself, with -c you have to specify it yourself. Thus,



              sh -c 'echo Hi, I am $0 , my first positional parameter is $1' foobar 5


              would set the process name to sh foobar.



              Just in case you're wondering what $0 is, it's also covered in "Special Parameters" section of Shell Command Language Specifications:




              0



              (Zero.) Expands to the name of the shell or shell script.







              share|improve this answer


























                4












                4








                4






                This behavior is actually specified by POSIX standard, which all Bourne-like shells should support to claim themselves portable.




                sh -c [-abCefhimnuvx] [-o option]... [+abCefhimnuvx] [+o option]...
                command_string [command_name [argument...]]




                See the command_string parameter ? Now let's look at -c flag description:




                -c



                Read commands from the command_string operand. Set the value of special parameter 0 (see Special Parameters) from the value of the command_name operand and the positional parameters ($1, $2, and so on) in sequence from the remaining argument operands. No commands shall be read from the standard input.




                In other words, where in normal shell script $0 (which is usually shell name in interactive mode or script name when you run a script) would be set by the shell itself, with -c you have to specify it yourself. Thus,



                sh -c 'echo Hi, I am $0 , my first positional parameter is $1' foobar 5


                would set the process name to sh foobar.



                Just in case you're wondering what $0 is, it's also covered in "Special Parameters" section of Shell Command Language Specifications:




                0



                (Zero.) Expands to the name of the shell or shell script.







                share|improve this answer














                This behavior is actually specified by POSIX standard, which all Bourne-like shells should support to claim themselves portable.




                sh -c [-abCefhimnuvx] [-o option]... [+abCefhimnuvx] [+o option]...
                command_string [command_name [argument...]]




                See the command_string parameter ? Now let's look at -c flag description:




                -c



                Read commands from the command_string operand. Set the value of special parameter 0 (see Special Parameters) from the value of the command_name operand and the positional parameters ($1, $2, and so on) in sequence from the remaining argument operands. No commands shall be read from the standard input.




                In other words, where in normal shell script $0 (which is usually shell name in interactive mode or script name when you run a script) would be set by the shell itself, with -c you have to specify it yourself. Thus,



                sh -c 'echo Hi, I am $0 , my first positional parameter is $1' foobar 5


                would set the process name to sh foobar.



                Just in case you're wondering what $0 is, it's also covered in "Special Parameters" section of Shell Command Language Specifications:




                0



                (Zero.) Expands to the name of the shell or shell script.








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 2 days ago

























                answered Jan 7 at 6:41









                Sergiy KolodyazhnyySergiy Kolodyazhnyy

                70.4k9146307




                70.4k9146307






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Ask Ubuntu!


                    • 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%2faskubuntu.com%2fquestions%2f1107617%2fhow-do-i-use-command-line-arguments-to-sh-in-the-c-command-string%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

                    If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                    Alcedinidae

                    Can an atomic nucleus contain both particles and antiparticles? [duplicate]