Mohammedz.com

For Linux and Shell scripting.

Array manipulation in shell scripting

Leave a comment


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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s