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

Related posts:

  1. Open file and Splitting String in Perl What I gonna do is to read a bunch of...
  2. Grep Exim Email Transaction with Bash While email transport having problem, the first we look for...
  3. array_merge Having problem during coding, was thinking how to join an...
  4. screen in FreeBSD default to csh as suppose to bash (preferred) After changing bash shell to a new user using chsh,...

Leave a Reply