45.34 Arrays in the Bourne Shell
The
C shell (47.5
)
,
awk
(33.11
)
,
the Korn shell, and some other UNIX command
interpreters have built-in array support.
The standard Bourne shell doesn't, though its
command line is a sort-of array that you can store with the
set
(44.19
)
command - and get stored values through
You can store and use Bourne shell variables - with names like array1
,
array2
, and so on - to simulate an array with elements 1, 2, and so on.
The
eval
(8.10
)
command does the trick.
As an example, if the n
shell variable stores the array index
( eval part$n=" and use its value with: eval echo "The part is \$part$n." You need the extra quoting in that last command because eval
scans the
command line twice.
The really important part is echo "The part is $part5." The next pass gives the value of the part5 variable. To store a line of text with multiple words into these fake array elements, the set command won't work. A for loop (44.16 ) usually will. For example, to read a line of text into the temp variable and store it in an "array" named part :
The first word from - |
|