Arrays
Both shells support one-dimensional arrays . The first element is numbered 0. Bash has no limit on the number of elements. ksh88 allowed up 1024 elements, early versions of ksh93 allowed at least 4096 elements, and modern versions allow up to 65,536 elements. Arrays are initialized with a special form of assignment:
message=(hi there how are you today) Bash and ksh93where the specified values become elements of the array. The Korn shell has an additional syntax:
set -A message hi there how are you today Ksh88 and ksh93Individual elements may also be assigned to:
message[0]=hi This is the hard way
message[1]=there
message[2]=how
message[3]=are
message[4]=you
message[5]=todayDeclaring arrays is not required. Any valid reference to a subscripted variable can create an array.
When referencing arrays , use the ${ ...
} syntax. This isn’t needed when
referencing arrays inside (())
(the form of let that does
automatic quoting). Note that [
and ] are typed literally (i.e.,
they don’t stand for optional syntax).
|
|
Use element i of array name. i can be any arithmetic expression as described under let. |
|
|
Use element 0 of array name. |
|
|
Use all elements of array name. |
|
|
Same. |
|
|
Use the number of elements in array name. |
|
|
Same. |
ksh93 provides associative
arrays , where the indices are strings instead of numbers
(as in awk). In this case,
[ and ] act like double quotes. Associative arrays are created ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access