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