DEV Community

Cover image for Bash: All you need to know to work with bash arrays
Rehan Qadir
Rehan Qadir

Posted on

Bash: All you need to know to work with bash arrays

Indexed Arrays

We start with simple indexed arrays in bash. You can define an indexed array by using parentheses and assignment operator. Lets say you wish to store names of months in months variable as an array. Here is how you will do it in bash.

months=("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")
Enter fullscreen mode Exit fullscreen mode

Bash arrays start with the index 0 so to retrieve the value for October we will use index 9, as in the following code.

months=("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")

echo ${months[9]}
# Output: Oct

index=5

echo ${months[$index]}
#Output: Jun
Enter fullscreen mode Exit fullscreen mode

Make sure to use curly braces when accessing an element through index.

Bash allow use of special characters to get all values and indexes of an array at once. Use * or @ to get all values stored in an array and ! before array name to get all indexes. And to get length of an array use #.

months=("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")

# Get all values from months array
echo ${months[*]}
# or echo ${months[@]}
# Output: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

# Get all indexes
echo ${!months[@]}
# or echo ${!months[*]}
# Output: 0 1 2 3 4 5 6 7 8 9 10 11

# Length of array
echo ${#months[@]}
# or echo ${#months[*]}
# Output: 12
Enter fullscreen mode Exit fullscreen mode

You can easily append data to an existing indexed array using += operator or you can specify the index by your own.

months=("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")

# Add to the months array
months+=("Raj" "Sha" "Ra1" "Ra2")

echo ${months[@]}
# Output: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Raj Sha Ra1 Ra2

# Add element to any specified index
months[20]="Muh"
echo ${months[@]}
# Output: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Raj Sha Ra1 Ra2 Muh

# Get all indexes
echo ${!months[@]}
# Output: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 20

# Initialize second array
animals=("Cow" "Goat" "Horse" "Lion" [11]="Brock Lesnar")

# Get all indexes for animals
echo ${!animals[@]}
# Output: 0 1 2 3 11
Enter fullscreen mode Exit fullscreen mode

Notice that you can specify an index out of order as well and bash will take care of it. As it can be seen that we have added Muh at the 20th index and it worked just fine. Also, while initiating animals array we changed the indexing deliberately and it also worked without any error.

Keep note that negative indexes are not allowed.

Associative Arrays or Hashes

Bash also supports hashes that is storing data as key => value pair. You can all it associative array if you are coming from PHP background or dictionary in Python.

We can declare a variable to be an associative array by using declare -A <variable name> command. We can also initialize it using parentheses at the time of declaration.

declare -A stars=([ALP]="Cow" [BET]="Bow" [GAM]="Goat" [DEL]="Scorpion"
[EPS]="Balance" [ZET]="Queen" [ETA]="Lion" [THE]="Fish")

# OR
declare -A planets
planets["first"]="Mercury"
planets["second"]="Venus"
planets["third"]="Earth"
planets["Musk adventure"]="Mars"
planets["second last"]="Uranus"
Enter fullscreen mode Exit fullscreen mode

We can access the keys and values of an associative array in a similar fashion as of indexed arrays.

We will print the key value pairs of an associative array using for loop to conclude this topic.

declare -A stars=([ALP]="Cow" [BET]="Bow" [GAM]="Goat" [DEL]="Scorpion"
[EPS]="Balance" [ZET]="Queen" [ETA]="Lion" [THE]="Fish")

for key in ${!stars[@]}; do
    echo "$key => ${stars[$key]}"
done

# Output
# GAM => Goat
# EPS => Balance
# ALP => Cow
# ZET => Queen
# DEL => Scorpion
# BET => Bow
# ETA => Lion
# THE => Fish

# Get length of stars
echo ${#start[@]}
# Output: 8
Enter fullscreen mode Exit fullscreen mode

That's it for Arrays in bash from my side.


I hope it will help you. Keep Coding!

Top comments (3)

Collapse
 
waylonwalker profile image
Waylon Walker

This syntax is so terse it makes it hard to follow without your descriptions. Thanks for sharing, I will definitely reference this if I have any bash array needs.

Collapse
 
drraq profile image
Rehan Qadir

Thanks Walker. Bash arrays are very useful utility for System Administrators in automation tasks.

Collapse
 
fenix profile image
Fenix

Thanks for sharing.

Report : some syntax error on 'echo ${#start[@]}'
seems to me ?
shouldn't it be 'echo ${#stars[I@]}'
I love astronomy, BTW ! xD