Rahul's Blog

Bash Shell Scripting: Arrays

July 21, 2020

#!/bin/bash

# bash arrays can only have 1 dimesion and indexes start at 0

# create an array
fav_nums=(3.14 2.718 0.57721 4.6692)
echo "PI: ${fav_nums[0]}"

# add value to array
fav_nums[4]=1.618
echo "GR: ${fav_nums[4]}"

# add group of values to array
fav_nums+=(1 7)

# output all array values
for i in ${fav_nums[*]}; do
    echo $i
done

# output indexes
for i in ${!fav_nums[@]}; do
    echo $i
done

# get number of items in array
echo "array length: ${#fav_nums[@]}"

# get length of array element
echo "Index 3 length: ${#fav_nums[3]}"

# sort an array
sorted_nums=($(for i in "${fav_nums[@]}"; do
    echo $i;
done | sort))

for i in ${sorted_nums[*]}; do
    echo $i;
done 
# delete array element
unset 'sorted_nums[1]'

# delete array
unset sorted_nums

Written by Rahul Jain         
I write code like I make sandwich