Skip to content
Search

Bash:Array

lastmod 2025-02-14created 2015-02-1210 edits

Newer versions of bash support one-dimensional arrays. You can declare one like variable[xx], or specify it directly like declare -a variable. To dereference an array variable (to get its contents), use brace notation like ${variable[xx]}.

How to use

Bash arrays have numbered indexes only, but they are sparse, ie you don't have to define all the indexes. An entire array can be assigned by enclosing the array items in parenthesis:

arr=(Hello World)

Individual items can be assigned with the familiar array syntax (unless you're used to Basic or Fortran):

arr[0]=Hello
arr[1]=World

But it gets a bit ugly when you want to refer to an array item:

echo ${arr[0]} ${arr[1]}

In addition the following funky constructs are available:

${arr[*]}         # All of the items in the array
${arr[@]}         # == ${arr[*]}
${!arr[*]}        # All of the indexes in the array
${#arr[*]}        # Number of items in the array
${#arr[0]}        # Length of item zero

The ${!arr[*]} is a relatively new addition to bash, it was not part of the original array implementation.

The following example shows some simple array usage (note the "[index]=value" assignment to assign a specific index):

#!/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]}
done

Append array

arrVar=("AC" "TV" "Mobile" "Fridge" "Oven" "Blender")

# Add new element at the end of the array
arrVar+=("Dish Washer")

# Add new element at the end of the array
arrVar[${#arrVar[@]}]="Python"

# Add new element at the end of the array
arrVar=(${arrVar[@]} "Jack Fruit")

Split to array

version='1.2.33'
a=( ${version//./ } )                   # replace points, split into array
((a[2]++))                              # increment revision (or other part)
version="${a[0]}.${a[1]}.${a[2]}"       # compose new version

Multi-dimensional array

declare -A URLS
URLS[0,0]="MY IP #50 Profile1"
URLS[0,1]="rtsp://192.168.0.50:554/media/1/1/Profile1"
URLS[1,0]="Localhost RTSP Server"
URLS[1,1]="rtsp://0.0.0.0:8554/live.sdp"
URLS[2,0]="Smatii DRONE wjcam003"
URLS[2,1]="rtsp://106.244.179.242:554/wjcam003"
URLS[3,0]="Smatii DRONE wjcam004"
URLS[3,1]="rtsp://106.244.179.242:554/wjcam004"
URLS_COUNT=$(( ${#URLS[*]} / 2 ))
URLS_MAX_INDEX=$(( URLS_COUNT - 1 ))

function print_urls
{
    for (( i = 0; i < URLS_COUNT; ++i )); do
        name=${URLS[$i,0]}
        url=${URLS[$i,1]}
        echo "[$i] $name: '$url'"
    done
}

print_urls

Example

The wget script I use:

#!/usr/bin/env bash

# Don't check the server certificate against the available certificate authorities.
FLAG[0]=--no-check-certificate

# Continue getting a partially-downloaded file.
FLAG[1]=--continue

# Turn on options suitable for mirroring.
# Equivalent to -r -N -l inf --no-remove-listing.
FLAG[2]=--mirror

# Identify as agent-string to the HTTP server.
#FLAG[3]=--user-agent=Wget

# In certain cases, the local file will be clobbered, or overwritten, upon repeated download.
FLAG[4]=--no-clobber

# After the download is complete, convert the links in the document to make them suitable for local viewing.
FLAG[5]=--convert-links

# Do not ever ascend to the parent directory when retrieving recursively.
#FLAG[6]=--no-parent

wget ${FLAG[*]} "$@"

See also

Favorite site

author Zer0 sitemap