blob: 729bd4db43cc02c1dced969d8df88cf95e9a3bb5 [file] [log] [blame]
Heinrich Schuchardt1b0c3162024-01-14 14:53:13 +01001.. index::
2 single: for (command)
3
Heinrich Schuchardt758adf42021-01-20 18:09:30 +01004for command
5===========
6
Heinrich Schuchardt44b09b32024-03-16 11:09:36 +01007Synopsis
8--------
Heinrich Schuchardt758adf42021-01-20 18:09:30 +01009
10::
11
12 for <variable> in <items>; do <commands>; done
13
14Description
15-----------
16
17The for command is used to loop over a list of values and execute a series of
18commands for each of these.
19
20The counter variable of the loop is a shell variable. Please, keep in mind that
21an environment variable takes precedence over a shell variable of the same name.
22
23variable
24 name of the counter variable
25
26items
27 space separated item list
28
29commands
30 commands to execute
31
32Example
33-------
34
35::
36
37 => setenv c
38 => for c in 1 2 3; do echo item ${c}; done
39 item 1
40 item 2
41 item 3
42 => echo ${c}
43 3
44 => setenv c x
45 => for c in 1 2 3; do echo item ${c}; done
46 item x
47 item x
48 item x
49 =>
50
51The first line ensures that there is no environment variable *c*. Hence in the
52first loop the shell variable *c* is printed.
53
54After defining an environment variable of name *c* it takes precedence over the
55shell variable and the environment variable is printed.
56
57Return value
58------------
59
60The return value $? after the done statement is the return value of the last
61statement executed in the loop.
62
63::
64
65 => for i in true false; do ${i}; done; echo $?
66 1
67 => for i in false true; do ${i}; done; echo $?
68 0