Home OS X
OS X Syntax

foreach

Loop, expand words, and execute commands

SYNTAX
       foreach name (wordlist)
       ...
       end     

Successively set the variable name to each member of wordlist and execute the sequence of commands between this command and the matching end.(Both foreach and end must appear alone on separate lines.)

The builtin command continue may be used to continue the loop prematurely and the builtin command break to terminate it prematurely.

When this command is read from the terminal, the loop is read once prompting with `foreach? ' (or prompt2) before any statements in the loop are executed. If you make a mistake typing in a loop at the terminal you can rub it out.

Example

foreach myloop (1 2 3 4 5)
  echo -n "$myloop"
end

`foreach' is a tcsh shell command.

"The big thieves hang the little ones" - Czech Proverb

Related commands:

break - Exit from a loop
repeat - Execute a command multiple times
while - Execute commands
continue - Resume the next iteration of a while or foreach loop.

Equivalent BASH command:

while - Execute commands



Back to the Top

Simon Sheppard
SS64.com