for(1) manual page
Table of Contents
for, foreach, repeat - shell built-in functions to repeatedly execute
action(s) for a selected number of times
for word [ in wordlist...
] ; do actions ; done
foreach word (wordlist)
...
end
repeat count command
for word [ in wordlist ... ] ; do actions ; done
SUNWcsu
Each time a for command is executed,
word is set to the next item taken from the in wordlist. If in wordlist
... is omitted, then the for command executes the do actions once for each
positional parameter that is set. Execution ends when there are no more
words in the list.
The variable word is successively set to each member
of wordlist. The sequence of commands between this command and the matching
end is executed for each new value of word. Both foreach and end must appear
alone on separate lines.
repeat executes command repeatedly count times.
count must be a number. command is restricted to a one-line statement.
Each
time a for command is executed, word is set to the next item taken from
the in wordlist. If in wordlist ... is omitted, then the for command executes
the do actions once for each positional parameter that is set. Execution
ends when there are no more words in the list.
The built-in
command continue may be used to terminate the execution of the current
iteration of a for or foreach loop, and the built-in command break may be
used to terminate execution of a for or foreach command.
In
the examples using for/foreach, the code counts the number of lines for
each file in the current directory whose name ends with a ".c" extension.
The repeat example prints "I will not chew gum in class" 500 times.
for file in *.c ; do wc -l $file ; done
foreach file ( *.c )
wc -l $file
end
for file in *.c ; do wc -l $file ; done
The repeat command re-executes the single subsequent command for count
number of times.
@ repetition = 500
repeat $repetition echo "I will not chew gum in class."
break(1)
, csh(1)
, ksh(1)
, sh(1)
Both the Bourne shell, sh,
and the Korn shell, ksh, can use the semicolon and the carriage return
interchangeably in their syntax of the if, for, and while built-in commands.
Table of Contents