I needed to know syntax for for
loop in bash in a certain range of numbers. I typed help for
but it didn't have this information. Then I learnt from some online article that the syntax is for i in {1..N}
. Is there a commandline help tool for bash that includes complete information about its usage?

- 5,335
-
Related: Reading and searching long man pages. – slm Jan 14 '14 at 02:06
1 Answers
for: for NAME [in WORDS ... ] ; do COMMANDS; done
# the syntax
Execute commands for each member in a list.
# the member list refers to the [in WORDS ... ] part.
# you see them in brackets because they are optional.
The `for` loop executes a sequence of commands for each member in a list of items
# the 'sequence of commands' refers to the COMMANDS part in the syntax.
If `in WORDS ...;` is not present, then `in "$@"` is assumed.
# if the optional [in WORDS ... ] isn't there, it will loop over each
# positional parameter.
For each element in WORDS, NAME is set to that element, and
the COMMANDS are executed.
Exit Status:
Returns the status of the last command executed.
The brace expansion
in your example isn't the syntax per 'se, but it does provide for the [in WORDS ... ]
of it's proper syntax. you could use anything that results in outputting words for it to be syntax correct, but depending on what you do, it may have unexpected results. Like attempting to read lines of a text file or deal with large numbers via brace expansion.
bash -c 'for x in {1..1000000000000}; do :; done'
bash: xmalloc: stringvec.c:40: cannot allocate 2909519884 bytes (94208 bytes allocated)
Since all 1000000000000
WORDS have to be expanded prior to the for loop running, if you don't have the ram for it, bash will error out.
There is also the c-style for loops that have the syntax of for (( exp1; exp2; exp3 )); do COMMANDS; done
which help for
also shows.
If you are interested in how to learn to script in bash, i would recommend reading: http://mywiki.wooledge.org/BashGuide

- 6,900
-
Thanks, but then from where do I learn about this particular brace expansion (any command for that)? – user13107 Jan 14 '14 at 01:53
-
2