Why does a for-loop not require explicitly specifying the set of values of the loop variable?











up vote
1
down vote

favorite












I read a command from https://unix.stackexchange.com/a/175845/674



Why can



$ bash -c 'for f do echo "$f";done' bash a b c
a
b
c


output the same as



$ bash -c 'for f in "$@"; do echo "$f";done' bash a b c
a
b
c


?



Why does a for-loop not require explicitly specifying




  • the set of values of the loop variable, as in "$@" and


  • ; ?



Thanks.










share|improve this question


























    up vote
    1
    down vote

    favorite












    I read a command from https://unix.stackexchange.com/a/175845/674



    Why can



    $ bash -c 'for f do echo "$f";done' bash a b c
    a
    b
    c


    output the same as



    $ bash -c 'for f in "$@"; do echo "$f";done' bash a b c
    a
    b
    c


    ?



    Why does a for-loop not require explicitly specifying




    • the set of values of the loop variable, as in "$@" and


    • ; ?



    Thanks.










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I read a command from https://unix.stackexchange.com/a/175845/674



      Why can



      $ bash -c 'for f do echo "$f";done' bash a b c
      a
      b
      c


      output the same as



      $ bash -c 'for f in "$@"; do echo "$f";done' bash a b c
      a
      b
      c


      ?



      Why does a for-loop not require explicitly specifying




      • the set of values of the loop variable, as in "$@" and


      • ; ?



      Thanks.










      share|improve this question













      I read a command from https://unix.stackexchange.com/a/175845/674



      Why can



      $ bash -c 'for f do echo "$f";done' bash a b c
      a
      b
      c


      output the same as



      $ bash -c 'for f in "$@"; do echo "$f";done' bash a b c
      a
      b
      c


      ?



      Why does a for-loop not require explicitly specifying




      • the set of values of the loop variable, as in "$@" and


      • ; ?



      Thanks.







      bash






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 hours ago









      Tim

      25.1k72243444




      25.1k72243444






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote













          From the bash man page:




          for name [ [ in [ word ... ] ] ; ] do list ; done



          The list of words
          following in is expanded, generating a list of items. The variable
          name is set to each element of this list in turn, and list is executed
          each time. If the in word is omitted, the for command executes list
          once for each positional parameter that is set
          (see PARAMETERS below). ...




          Then from the parameters section:




          A positional parameter is a parameter denoted by one or more digits,
          other than the single digit 0. Positional parameters are assigned from
          the shell's arguments when it is invoked
          , and may be reassigned using
          the set builtin command. ...




          In summary, the in keyword and following semicolon are optional. If you omit the in keyword, the parameters for the for loop are taken from the arguments passed to the shell. Therefore, the two code samples you provide are equivalent.






          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: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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%2funix.stackexchange.com%2fquestions%2f485434%2fwhy-does-a-for-loop-not-require-explicitly-specifying-the-set-of-values-of-the-l%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote













            From the bash man page:




            for name [ [ in [ word ... ] ] ; ] do list ; done



            The list of words
            following in is expanded, generating a list of items. The variable
            name is set to each element of this list in turn, and list is executed
            each time. If the in word is omitted, the for command executes list
            once for each positional parameter that is set
            (see PARAMETERS below). ...




            Then from the parameters section:




            A positional parameter is a parameter denoted by one or more digits,
            other than the single digit 0. Positional parameters are assigned from
            the shell's arguments when it is invoked
            , and may be reassigned using
            the set builtin command. ...




            In summary, the in keyword and following semicolon are optional. If you omit the in keyword, the parameters for the for loop are taken from the arguments passed to the shell. Therefore, the two code samples you provide are equivalent.






            share|improve this answer



























              up vote
              2
              down vote













              From the bash man page:




              for name [ [ in [ word ... ] ] ; ] do list ; done



              The list of words
              following in is expanded, generating a list of items. The variable
              name is set to each element of this list in turn, and list is executed
              each time. If the in word is omitted, the for command executes list
              once for each positional parameter that is set
              (see PARAMETERS below). ...




              Then from the parameters section:




              A positional parameter is a parameter denoted by one or more digits,
              other than the single digit 0. Positional parameters are assigned from
              the shell's arguments when it is invoked
              , and may be reassigned using
              the set builtin command. ...




              In summary, the in keyword and following semicolon are optional. If you omit the in keyword, the parameters for the for loop are taken from the arguments passed to the shell. Therefore, the two code samples you provide are equivalent.






              share|improve this answer

























                up vote
                2
                down vote










                up vote
                2
                down vote









                From the bash man page:




                for name [ [ in [ word ... ] ] ; ] do list ; done



                The list of words
                following in is expanded, generating a list of items. The variable
                name is set to each element of this list in turn, and list is executed
                each time. If the in word is omitted, the for command executes list
                once for each positional parameter that is set
                (see PARAMETERS below). ...




                Then from the parameters section:




                A positional parameter is a parameter denoted by one or more digits,
                other than the single digit 0. Positional parameters are assigned from
                the shell's arguments when it is invoked
                , and may be reassigned using
                the set builtin command. ...




                In summary, the in keyword and following semicolon are optional. If you omit the in keyword, the parameters for the for loop are taken from the arguments passed to the shell. Therefore, the two code samples you provide are equivalent.






                share|improve this answer














                From the bash man page:




                for name [ [ in [ word ... ] ] ; ] do list ; done



                The list of words
                following in is expanded, generating a list of items. The variable
                name is set to each element of this list in turn, and list is executed
                each time. If the in word is omitted, the for command executes list
                once for each positional parameter that is set
                (see PARAMETERS below). ...




                Then from the parameters section:




                A positional parameter is a parameter denoted by one or more digits,
                other than the single digit 0. Positional parameters are assigned from
                the shell's arguments when it is invoked
                , and may be reassigned using
                the set builtin command. ...




                In summary, the in keyword and following semicolon are optional. If you omit the in keyword, the parameters for the for loop are taken from the arguments passed to the shell. Therefore, the two code samples you provide are equivalent.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 1 hour ago

























                answered 1 hour ago









                Peschke

                2,205923




                2,205923






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • 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%2funix.stackexchange.com%2fquestions%2f485434%2fwhy-does-a-for-loop-not-require-explicitly-specifying-the-set-of-values-of-the-l%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

                    數位音樂下載

                    When can things happen in Etherscan, such as the picture below?

                    格利澤436b