Home > Articles > Operating Systems, Server > Linux/UNIX/Open Source

  • Print
  • + Share This
This chapter is from the book

Array Variables

One-dimensional arrays are supported by the Korn shell. Arrays can have a maximum of 4096 elements. Array subscripts start at 0 and go up to 4096 (or the maximum element minus one). Any variable can become a one-dimensional array by simply referring to it with a subscript. Here, variable X is set to A:

$ X=A 

By explicitly assigning X[1] a value, variable X is transformed into an array variable:

$ X[1]=B 

The original assignment to variable X is not lost. The first array element (X[0]) is still assigned A.

Table 3.6. More Variable Expansion Formats

${variable//pattern1/pattern2}

replace all occurrences of pattern1 with pattern2 in variable

${variable/pattern1/pattern2}

replace first occurrence of pattern1 with pattern2 in variable

${variable/#pattern1/pattern2}

replace first occurrence of pattern1 with pattern2 in variable if variable begins with pattern1

${variable/%pattern1/pattern2}

replace last occurrence of pattern1 with pattern2 in variable if variable ends with pattern1

${variable:start}

return variable from character position start to end

${variable:start:length}

return length characters from variable from character position start to end

Array Variable Assignments and Declarations

Arrays can be assigned values by using normal variable assignment statements, the set –A command, or the typeset command:

variable[0]=value variable[1]=value. . . variable[n]=value 

or

set –A variable value0 value1 . . . valuen 

or

typeset variable[0]=value variable[1]=value. . . \ 
   variable[n]=value 

The only difference between the formats is that with the set command format, the values are assigned to the array variable sequentially starting from element zero. In the other formats, the array elements can be assigned values in any order. This example assigns the weekday names to the array variable DAY using normal variable assignment format:

$ DAY[0]=Mon DAY[1]=Tue DAY[2]=Wed  \ 
   DAY[3]=Thu DAY[4]=Fri DAY[5]=Sat DAY[6]=Sun 

The same variable can be assigned values using the set command:

$ set —A DAY Mon Tue Wed Thu Fri Sat Sun 

or the typeset command:

$ typeset DAY[0]=Mon DAY[1]=Tue DAY[2]=Wed \ 
   DAY[3]=Thu DAY[4]=Fri DAY[5]=Sat DAY[6]=Sun 

Not all the elements of an array need to exist. You can assign values to non-successive elements of an array. Here, the first and fourth elements of the TEST array are assigned values.

$ typeset TEST[0]=a TEST[3]=b 

Table 3.7. Array Variables

${array}, $array

array element zero

${array[n]}

array element n

${array[n+2]}

array element n+2

${array[$i]}

array element $i

${array[*]}, ${array[@]}

all elements of an array

${#array[*]}, ${#array[@]}

number of array elements

${#array[n]

length of array element n

${!array[*]}, ${!array[@]}

all initialized subscript values

${!array[*]:n:x}

x array elements starting withelement n

${!array[@]:n}

all array elements starting with element n

Array Variable Expansion

Array variables are expanded in the same manner as normal variables and parameters: using the $ character. Without a subscript value, an array variable refers to the first element, or element 0.

$ print $DAYS is the same as $DAY[0] 
   Mon is the same as Mon 

To access the value of a specific array variable element use a subscript. Array variable names and subscripts must be enclosed in braces for proper expansion:

$ print ${DAY[3]} ${DAY[5]} 
   Thu Sat 

If an element does not exist, nothing is substituted:

$ print ${DAY[25]} 
   
   $ 

All the elements of an array can be accessed by using the * or @ as the subscript value. They both return the same value. These examples print the values of all the elements of the DAY array variable:

$ print ${DAY[*]} 
   Mon Tue Wed Thu Fri Sat Sun 
   $ print ${DAY[@]} 
   Mon Tue Wed Thu Fri Sat Sun 

The number of elements of an array variable is returned by using the # in front of the array variable name and using * or @ as the subscript value. Let's try it with DAY:

$ print ${#DAY[*]} 
   7 
   $ print ${#DAY[@]} 
   7 

To get values for a subset of an array, use this format:

${variable[*]:start_subscript:num_elements} 

or

${variable[@]:start_subscript} 

Arithmetic expressions can be used to return a subscript value. This example prints the fifth element of the DAY array variable. Remember that array subscripts start with 0, so the third array element has a subscript of 2, and the fifth element has a subscript of 4:

$ print ${DAY[4/2]} 
   Wed 
   $ print ${DAY[7-6+5-4+3-2+1] 
   Fri 

Variable expansion can also be used to generate a subscript value.

$ X=6 
   $ print ${DAY[$X]} 
   Sun 

Array Variable Attributes

As with ordinary variables, attributes can be assigned to array-type variables. Arrays can also be declared, and assigned values and attributes with the typeset command:

typeset –attribute variable[0]=value variable[1]=value. . . 

Once set, attributes apply to all elements of an array. This example sets the uppercase attribute for the DAY array variable using the typeset –u command:

$ typeset —u DAY 

Now all the element values are displayed in upper case:

$ print ${DAY[*]} 
   MON TUE WED THU FRI SAT SUN 

Array element attributes can be set before or after assignment. Here, XVAR is initially assigned lowercase aaa, bbb, and ccc:

$ set —A XVAR aaa bbb ccc 
   $ print ${XVAR[*]} 
   aaa bbb ccc 

Now, the uppercase, left-justify, two-character-wide attributes are set and the new element values are displayed. Notice that the third character from each element has been dropped, and the value is now in uppercase:

$ typeset —uL2 XVAR 
   $ print ${XVAR[*]} 
   AA BB CC 

Array Variable Reassignments

Besides using regular array-element[n]=value or typeset array-element[n]=value syntax to reassign values, array variables can also have their values reassigned with the set +A command:

set +A variable value0 value1 . . . 

This is helpful when you don't want to reassign values to all the elements of an array. In this example, the array variable X is assigned six values:

$ set —A X one two three d e f 
   $ print ${X[*]} 
   one two three d e f 

Using the set +A command, the first three elements are reassigned a, b, and c:

$ set +A X a b c 
   $ print ${X[*]} 
   a b c d e f 

Notice that values of the fourth, fifth, and sixth elements have not been affected.

Associative Arrays

This version of the Korn shell also supports associative arrays, that is arrays that use string subscripts rather than integer subscripts. Associative arrays are declared using this format:

typeset A variable 

where variable is the name of the associative array. Additional arguments can be given to the typeset command to specify a data type. For example, we can create an associative array to store some exchange rates:

$ typeset -AE exchange_rate 
   $  exchange_rate["DM"]=1.7 
   $  exchange_rate["FF"]=.15 
   $  exchange_rate["AS"]=.04 

To display a list of associative array subscripts:

${!variable[*]} or ${!variable[@]} 

To display the values for all or parts of an associative array:

${!variable[subscript]} 

For example, all and a specific exchange rate is displayed here:

$ print ${!exchange_rate[*]} 
   0.15 1.7 
   $ print "The DM exchange rate is:${exchange_rate[DM]}" 
   1.7 
  • + Share This
  • 🔖 Save To Your Account