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











up vote
1
down vote

favorite













This question already has an answer here:




  • Bash “for” loop without a “in foo bar…” part

    2 answers




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













marked as duplicate by muru, Thomas, RalfFriedl, Jeff Schaller, G-Man Dec 2 at 18:49


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















    up vote
    1
    down vote

    favorite













    This question already has an answer here:




    • Bash “for” loop without a “in foo bar…” part

      2 answers




    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













    marked as duplicate by muru, Thomas, RalfFriedl, Jeff Schaller, G-Man Dec 2 at 18:49


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite












      This question already has an answer here:




      • Bash “for” loop without a “in foo bar…” part

        2 answers




      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














      This question already has an answer here:




      • Bash “for” loop without a “in foo bar…” part

        2 answers




      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.





      This question already has an answer here:




      • Bash “for” loop without a “in foo bar…” part

        2 answers








      bash






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 2 at 4:07









      Tim

      25.4k74244447




      25.4k74244447




      marked as duplicate by muru, Thomas, RalfFriedl, Jeff Schaller, G-Man Dec 2 at 18:49


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by muru, Thomas, RalfFriedl, Jeff Schaller, G-Man Dec 2 at 18:49


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          Because the POSIX grammar allows it.




          The format for the for loop is as follows:



          for name [ in [word ... ]]
          do
          compound-list
          done


          First, the list of words following in shall be expanded to generate a list of items. Then, the variable name shall be set to each item, in turn, and the compound-list executed each time. If no items result from the expansion, the compound-list shall not be executed. Omitting:



          in word...


          shall be equivalent to:



          in "$@"



          The formal grammar for the for loop looks like



          for_clause       : For name                                      do_group
          | For name sequential_sep do_group
          | For name linebreak in sequential_sep do_group
          | For name linebreak in wordlist sequential_sep do_group


          For is a grammar token for the string for. sequential_sep is either ; or one or several newlines and linebreak is a single optional newline. The do_group at the end is your do ...; done.



          This means that the valid for loops are





          1. Loop over "$@":



            for name do ...; done



          2. Loop over "$@":



            for name; do ...; done



          3. Loop over empty list:



            for name in; do ...; done



          4. Loop over non-empty list:



            for name in word-list; do ...; done



          The third form is valid but doesn't do anything. It exists to allow loops whose word-list expands to nothing.






          share|improve this answer






























            up vote
            7
            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























            • If the positional parameters are set with set within the script, the loop without the list will not loop over the arguments of the script, but over the new positional parameters.
              – Kusalananda
              Dec 2 at 9:02


















            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote



            accepted










            Because the POSIX grammar allows it.




            The format for the for loop is as follows:



            for name [ in [word ... ]]
            do
            compound-list
            done


            First, the list of words following in shall be expanded to generate a list of items. Then, the variable name shall be set to each item, in turn, and the compound-list executed each time. If no items result from the expansion, the compound-list shall not be executed. Omitting:



            in word...


            shall be equivalent to:



            in "$@"



            The formal grammar for the for loop looks like



            for_clause       : For name                                      do_group
            | For name sequential_sep do_group
            | For name linebreak in sequential_sep do_group
            | For name linebreak in wordlist sequential_sep do_group


            For is a grammar token for the string for. sequential_sep is either ; or one or several newlines and linebreak is a single optional newline. The do_group at the end is your do ...; done.



            This means that the valid for loops are





            1. Loop over "$@":



              for name do ...; done



            2. Loop over "$@":



              for name; do ...; done



            3. Loop over empty list:



              for name in; do ...; done



            4. Loop over non-empty list:



              for name in word-list; do ...; done



            The third form is valid but doesn't do anything. It exists to allow loops whose word-list expands to nothing.






            share|improve this answer



























              up vote
              4
              down vote



              accepted










              Because the POSIX grammar allows it.




              The format for the for loop is as follows:



              for name [ in [word ... ]]
              do
              compound-list
              done


              First, the list of words following in shall be expanded to generate a list of items. Then, the variable name shall be set to each item, in turn, and the compound-list executed each time. If no items result from the expansion, the compound-list shall not be executed. Omitting:



              in word...


              shall be equivalent to:



              in "$@"



              The formal grammar for the for loop looks like



              for_clause       : For name                                      do_group
              | For name sequential_sep do_group
              | For name linebreak in sequential_sep do_group
              | For name linebreak in wordlist sequential_sep do_group


              For is a grammar token for the string for. sequential_sep is either ; or one or several newlines and linebreak is a single optional newline. The do_group at the end is your do ...; done.



              This means that the valid for loops are





              1. Loop over "$@":



                for name do ...; done



              2. Loop over "$@":



                for name; do ...; done



              3. Loop over empty list:



                for name in; do ...; done



              4. Loop over non-empty list:



                for name in word-list; do ...; done



              The third form is valid but doesn't do anything. It exists to allow loops whose word-list expands to nothing.






              share|improve this answer

























                up vote
                4
                down vote



                accepted







                up vote
                4
                down vote



                accepted






                Because the POSIX grammar allows it.




                The format for the for loop is as follows:



                for name [ in [word ... ]]
                do
                compound-list
                done


                First, the list of words following in shall be expanded to generate a list of items. Then, the variable name shall be set to each item, in turn, and the compound-list executed each time. If no items result from the expansion, the compound-list shall not be executed. Omitting:



                in word...


                shall be equivalent to:



                in "$@"



                The formal grammar for the for loop looks like



                for_clause       : For name                                      do_group
                | For name sequential_sep do_group
                | For name linebreak in sequential_sep do_group
                | For name linebreak in wordlist sequential_sep do_group


                For is a grammar token for the string for. sequential_sep is either ; or one or several newlines and linebreak is a single optional newline. The do_group at the end is your do ...; done.



                This means that the valid for loops are





                1. Loop over "$@":



                  for name do ...; done



                2. Loop over "$@":



                  for name; do ...; done



                3. Loop over empty list:



                  for name in; do ...; done



                4. Loop over non-empty list:



                  for name in word-list; do ...; done



                The third form is valid but doesn't do anything. It exists to allow loops whose word-list expands to nothing.






                share|improve this answer














                Because the POSIX grammar allows it.




                The format for the for loop is as follows:



                for name [ in [word ... ]]
                do
                compound-list
                done


                First, the list of words following in shall be expanded to generate a list of items. Then, the variable name shall be set to each item, in turn, and the compound-list executed each time. If no items result from the expansion, the compound-list shall not be executed. Omitting:



                in word...


                shall be equivalent to:



                in "$@"



                The formal grammar for the for loop looks like



                for_clause       : For name                                      do_group
                | For name sequential_sep do_group
                | For name linebreak in sequential_sep do_group
                | For name linebreak in wordlist sequential_sep do_group


                For is a grammar token for the string for. sequential_sep is either ; or one or several newlines and linebreak is a single optional newline. The do_group at the end is your do ...; done.



                This means that the valid for loops are





                1. Loop over "$@":



                  for name do ...; done



                2. Loop over "$@":



                  for name; do ...; done



                3. Loop over empty list:



                  for name in; do ...; done



                4. Loop over non-empty list:



                  for name in word-list; do ...; done



                The third form is valid but doesn't do anything. It exists to allow loops whose word-list expands to nothing.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 6 at 21:20

























                answered Dec 2 at 8:38









                Kusalananda

                120k16225369




                120k16225369
























                    up vote
                    7
                    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























                    • If the positional parameters are set with set within the script, the loop without the list will not loop over the arguments of the script, but over the new positional parameters.
                      – Kusalananda
                      Dec 2 at 9:02















                    up vote
                    7
                    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























                    • If the positional parameters are set with set within the script, the loop without the list will not loop over the arguments of the script, but over the new positional parameters.
                      – Kusalananda
                      Dec 2 at 9:02













                    up vote
                    7
                    down vote










                    up vote
                    7
                    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 Dec 2 at 5:07

























                    answered Dec 2 at 4:33









                    Peschke

                    2,435924




                    2,435924












                    • If the positional parameters are set with set within the script, the loop without the list will not loop over the arguments of the script, but over the new positional parameters.
                      – Kusalananda
                      Dec 2 at 9:02


















                    • If the positional parameters are set with set within the script, the loop without the list will not loop over the arguments of the script, but over the new positional parameters.
                      – Kusalananda
                      Dec 2 at 9:02
















                    If the positional parameters are set with set within the script, the loop without the list will not loop over the arguments of the script, but over the new positional parameters.
                    – Kusalananda
                    Dec 2 at 9:02




                    If the positional parameters are set with set within the script, the loop without the list will not loop over the arguments of the script, but over the new positional parameters.
                    – Kusalananda
                    Dec 2 at 9:02



                    Popular posts from this blog

                    Quarter-circle Tiles

                    build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

                    Mont Emei