Looping over shell words with for

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
' "${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 loop is not "scoped" in any meaningful sense. If this variable had a value assigned before the loop started, it will be lost. After the loop finishes, it will remain assigned to the final item, in this case the "windows" string.

The in for for is only required if you want to specify a word list inline. Otherwise, the for loop defaults to the list of arguments to the current shell or function, that is, the positional parameters, expandable as "$@". We can test this out by assigning the parameters for the current shell; an alternative (but not exactly equivalent) way to write this might be as follows:

set -- bsd linux macosx windows
for system ; do
    printf '%s
' "${system^^}"
done

We could write the second line like this instead, if we wanted to be explicit:

for system in "$@" ; do

The for loop is hence the correct choice for iterating over an arbitrary number of arguments on which your script or function is running. It's also the correct choice for iterating over arrays, which we can think of as storage for an arbitrary number of shell words:

#!/bin/bash
systems=(bsd linux macosx windows) for system in "${systems[@]}" ; do printf '%s ' "${system^^}" done

This uses the special array subscript of @ that we discussed in our introduction to arrays at the end of Chapter 5, Variables and Patterns, expanding to every value in the array. Don't forget the all-important double quotes around the expansion, too: "${systems[@]}", never ${systems[@]}.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.125.76