Heinrich Schuchardt | 1b0c316 | 2024-01-14 14:53:13 +0100 | [diff] [blame] | 1 | .. index:: |
| 2 | single: for (command) |
| 3 | |
Heinrich Schuchardt | 758adf4 | 2021-01-20 18:09:30 +0100 | [diff] [blame] | 4 | for command |
| 5 | =========== |
| 6 | |
Heinrich Schuchardt | 44b09b3 | 2024-03-16 11:09:36 +0100 | [diff] [blame] | 7 | Synopsis |
| 8 | -------- |
Heinrich Schuchardt | 758adf4 | 2021-01-20 18:09:30 +0100 | [diff] [blame] | 9 | |
| 10 | :: |
| 11 | |
| 12 | for <variable> in <items>; do <commands>; done |
| 13 | |
| 14 | Description |
| 15 | ----------- |
| 16 | |
| 17 | The for command is used to loop over a list of values and execute a series of |
| 18 | commands for each of these. |
| 19 | |
| 20 | The counter variable of the loop is a shell variable. Please, keep in mind that |
| 21 | an environment variable takes precedence over a shell variable of the same name. |
| 22 | |
| 23 | variable |
| 24 | name of the counter variable |
| 25 | |
| 26 | items |
| 27 | space separated item list |
| 28 | |
| 29 | commands |
| 30 | commands to execute |
| 31 | |
| 32 | Example |
| 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 | |
| 51 | The first line ensures that there is no environment variable *c*. Hence in the |
| 52 | first loop the shell variable *c* is printed. |
| 53 | |
| 54 | After defining an environment variable of name *c* it takes precedence over the |
| 55 | shell variable and the environment variable is printed. |
| 56 | |
| 57 | Return value |
| 58 | ------------ |
| 59 | |
| 60 | The return value $? after the done statement is the return value of the last |
| 61 | statement 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 |