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,
1 2 3 4 5 6 7 |
declare -a HELLO |
To assign values into arrays
1 2 3 4 5 6 7 |
HELLO[0] = 'world' |
To assign multiple values into arrays
1 2 3 4 5 6 7 |
HELLO=(hello world 'i am here') |
To dump command line data into arrays
1 2 3 4 5 6 7 |
PROCESSID=(`ps ax | grep httpd`) |
Output Values from Arrays
To output a value from an Array
1 2 3 4 5 6 7 8 9 |
HELLO[0] = 'world' echo ${HELLO[0]}} >> world |
Loop through an Arrays and Output all the values
1 2 3 4 5 6 7 8 9 10 11 12 |
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