Arrays are a collection of contiguous elements that can be accessed by a subscript.

An array’s syntax is:

array [subscript]=value

Shell start placement of values at the 0 element.

In this example,

$testa[0]=first $testa[1]=second $echo ${testa[1]}
$echo ${testa[*]}
first second

the array test a first two elements (0 and 1) are set to first and second. The following echo‘s , display the value of the 1 element and then the value of every array element as designated by the *.

Suppose, the below script name is

array=(jerry gen glan rahim)
len=${#array[*]} #Number of elements of the array

echo the array has $len members. They are:

while [ $i -lt $len ]; do
echo "$i: ${array[$i]}"
let i++

# Array Operations In Shell Scripting
echo "Adding "jack" to the end of the array" array=( "${array[@]}" "jack" )
echo "Listng all the elements in the array" echo "${array[@]}"
echo "Listng all the elements in the array" echo "${array[*]}" #One more way
echo "Deleting \"gen\""
unset array[1] #Same as array[1]=
echo "Now the array is"
echo "${array[@]}"
echo "length of 3rd element in the array" len1=${#array[2]}; echo $len
echo "Deleting the whole array"
unset array
echo "${array[@]}" #Array is empty
$ ./


The array has 4 members. They are:

0: jerry
1: gen
2: glan
3: rahim

Subscribe For More Content