Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.


Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.


Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.


Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Commands & Shells, Scripts

Korn Shell Arrays

Submitted by on January 24, 2008 – 11:13 am 6 Comments

Here are a few useful examples of how to use data arrays within Korn shell. Arrays are a great tool for storing user input and other data for quick, on-the-fly access. If, for example, you script repeatedly reads the same input file, you can speed things up by converting the file into an array (depending, of course, on the size of the file).

Create a simple array

set -A termnames gl35a t2000 s531 vt99 # array elements are separated by blanks, TABs, or NEWLINEs

set -A arrayname $(< filename) # where “filename” is the file that contains array values

typeset -A StateTax
StateTax[New Jersey]=0.06
print ${StateTax[New Jersey]}

Read the array

print ${#termnames[*]} #shows the number of elements in the array; “*” can be replaced by “@”

print ${termnames[*]} #shows all values

print ${termnames[0]} #shows the first value

for i in 0 3 4 #show values 0, 3, and 4
print ${termnames[$i]}

print ${termnames[3]} is equivalent to print ${termnames[2+1]}

Sample script 1

set -A termnames gl35a t2000 s531 vt99
select term in `print ${termnames[*]}`
if [[ -n $term ]] then
print “TERM is $TERM”

Read a file into an array, one line at a time

cat /var/log/messages | while read LINE
(( i = i + 1 ))

Print values from the array

i=0 #array element count begins with “0”
while [ $i -lt ${#termnames[*]} ] do
print ${termnames[$i]}
(( i = i + 1 ))

Put the output of a command into an array

set -A dt `date`

> print ${#dt[*]}
> print ${dt[*]}
Wed Jan 30 00:55:04 EST 2008

Sample script 2

# parms2array: store the passed parameters in an array
# source:
set -A parms $*
print “You supplied ${#parms[@]} parameters:”
while [ $i -le ${#parms[@]} ] do
print ${parms[$i]}
(( i=i+1 ))

> ./parms2array red orange yellow green blue indigo violet
You supplied 7 parameters:

Print Friendly, PDF & Email


  • vanvark83 says:

    Write an application that creates an array with 12 random numbers between 20 and 60. Show it in a row.
    c)Find the sum of all the array’s elements and the sum of only elements that are < 40
    d)Find the max of the array’s elements and its index
    Switch the max and min in this array. Show the array after the switching again

  • davemc74656 says:

    The passed array may be a string or integer. I simply want to compare if certain elements are equal to other elements and I could have arrays defined with different data types. Any ideas on how to do this?

  • Milk84 says:

    also i need the code to initialize the first array element to a value of 10 and each succeeding element to a value of 3 times the previous element value, in a for loop..thanks

  • jdubdoubleu7704 says:

    If I have a class in Matlab with one property being array and the other ave. If I update the object’s array with [1,2,3,4,5], is there a way that if I enter “obj.ave” in the command line it will automatically display the average of the array elements without some intermediate step outside of the class file? Say, have a method that will be automatically invoked if I entered “obj.ave”.

  • skychi99 says:

    Eg : if array elements are 20,10,20,5,4 and i want to delete all occurrences of 20..and the desired output is 10,5,4

  • sick_mick_101 says:

    For example, I need to create an array of 20 elements and I have to put 40 digits inside EACH element. Is that possible with an int or another type of variable?

Leave a Reply

%d bloggers like this: