A common idiom in shell scripting is to tweak the value of
IFS (internal field separator) while reading lines of input:
1 2 3 4
This leads to questions about
IFS itself and the
-r flag, and there are plenty of good answers out there. I’d like to focus, however, on the syntax of
IFS= and it’s location in the above line.
Shell variables can be assigned and referenced:
1 2 3
Sometimes you want to set a variable for the duration of a single command:
1 2 3 4
At first glance, you might expect the first line of our
while loop to look like:
but this causes a syntax error. In the
name=Bob example, our entire line consisted of a single simple command, defined as
a sequence of optional variable assignments and redirections, in any sequence, optionally followed by words and redirections, terminated by a control operator.
while loop, however, is a compound command, with the format:
1 2 3 4
compound-list-1 being a sequence of lists. A list is defined as
a sequence of one or more AND-OR lists separated by the operators ‘;’ and ‘&’.
AND-OR list being
a sequence of one or more pipelines separated by the operators “&&” and “||” .
A pipeline, in turn, has the format:
[!] command1 [ | command2 …]
It feels like we’re going in circles, but the long and short of it is that we can view
Note that this means we’re setting
IFS to a temporary value only during the
read command, not during the body of the loop.
To make this a little more concrete, here’s a script I’ve called
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8