The for keyword is used to define code to run repeatedly over a list of shell words:
#!/bin/bash for system in bsd linux macosx windows ; do printf '%s\n' "${system^^}" done
The preceding code loops through the list of four items, assigning each one in turn to the system variable. The body of the loop prints the value of the system variable in all caps, for each iteration of the loop.
Note the following about the preceding syntax:
- The variable name goes just after for, and before in
- do is required and needs to be after the semicolon, to specify where the variable and list of words finishes
- The loop is closed with done, not rof as you might have expected from fi and esac
- The list of words can be blank; for system in ; do ... ; done is not a syntax error, it just does nothing
The assignment of the system variable in this...