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.
bash
add a comment |
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.
bash
add a comment |
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.
bash
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
bash
asked 3 hours ago
Tim
25.1k72243444
25.1k72243444
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited 2 hours ago
answered 2 hours ago
Peschke
2,205923
2,205923
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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