
Variables | 641
Bash and Korn
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
Individual 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]=today
Declaring 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 refer-
encing 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).
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 with typeset -A. A special syntax allows assigning to multiple
elements at once:
data=([joe]=30 [mary]=25)
The values would be retrieved as ${data[joe]} and ${data[mary]}.
Discipline Functions (ksh93 only)
Along with structured variables, ksh93 introduces discipline functions. These are
special functions that are called whenever a variable’s value is accessed or
changed. For a shell variable named x, you can define the following functions:
x.get
Called when x’s value is retrieved ($x).
x.set
Called when x’s value is changed (x=2).
x.unset
Called when x is unset (unset x).
Within the discipline functions, special ...