Arrays in Bash

Arrays is useful when it comes to data processing. Using Arrays in Bash is quite easy, and most importantly is to get your job done with the data loaded into the Arrays.

Declare and Assign Value into Array

To declare an array in bash,


declare -a HELLO

To assign values into arrays


HELLO[0] = 'world'

To assign multiple values into arrays


HELLO=(hello world 'i am here')

To dump command line data into arrays


PROCESSID=(`ps ax | grep httpd`)

Output Values from Arrays

To output a value from an Array


HELLO[0] = 'world'
echo ${HELLO[0]}}
>> world

Loop through an Arrays and Output all the values


HELLO=(hello world 'i am here')

for values in ${HELLO[*]}
do
    echo "${values}"
done

For more advance scripting on Arrays, visit http://tldp.org/LDP/abs/html/arrays.html