Mohammedz.com

For Linux and Shell scripting.


Leave a comment

Array manipulation in shell scripting

Best way to understand shell scripting is from examples. Find the example below to understand different options available in bash array manipulation.

You can down this script from here.
*********************************
#!/bin/bash

array=(one two three four [5]=five)

echo “Array size: ${#array[*]}”

echo “Array items:”
for item in ${array[*]}
do
printf ” %s\n” $item
done

echo “Array indexes:”
for index in ${!array[*]}
do
printf ” %d\n” $index
done

echo “Array items and indexes:”
for index in ${!array[*]}
do
printf “%4d: %s\n” $index ${array[index]}
#printf “%4d: %s\n” $index ${array[$index]}
done
*********************************

Regards,
Mohammed.