Bash Array(List) and Dict
Edit me

Bash Array and Dict

=====

Arrays

Defining Array

$ Fruits=('Apple' 'Banana' 'Orange')

$ echo "${Fruits[0]}"
Apple

$ echo "${Fruits[1]}"
Banana

$ echo "${Fruits[2]}"
Orange

$ for i in "${Fruits[@]}"; do echo "${i}"; done
Apple
Banana
Orange

Push an Element

$ Fruits=("${Fruits[@]}" "Waterlemon")

$ echo ${Fruits[@]}
Apple Banana Orange Waterlemon

$ Fruits+=('Cherry')

$ echo "${Fruits[@]}"
Apple Banana Orange Waterlemon Cherry

Remove an Element

$ echo "${Fruits[@]}"
Apple Banana Orange Waterlemon Cherry

$ unset Fruits[1]

$ echo "${Fruits[@]}"
Apple Orange Waterlemon Cherry

$ Fruits=(${Fruits[@]/App*/})

$ echo "${Fruits[@]}"
Orange Waterlemon Cherry

Duplicate / Concatenate array

$ echo "${Fruits[@]}"
Orange Waterlemon Cherry

$ AnotherFruits=("${Fruits[@]}")

$ echo "${AnotherFruits[@]}"
Orange Waterlemon Cherry

$ Fruits=("${Fruits[@]}" "${AnotherFruits[@]}")

$ echo "${Fruits[@]}"
Orange Waterlemon Cherry Orange Waterlemon Cherry

Read from File

$ cat /tmp/test
Apple
Orange
Cherry
Lemon

$ Fruits=($(cat /tmp/test))

$ echo "${Fruits[@]}"
Apple Orange Cherry Lemon

$ echo "${Fruits[1]}"
Orange

Length of Array

$ echo "${Fruits[@]}"
Apple Orange Cherry Lemon

$ echo "${#Fruits[@]}"
4

Length of an Element

$ echo "${Fruits[1]}"
Orange

$ echo "${#Fruits[1]}"
6

Slicing Array

$ echo "${Fruits[@]}"
Apple Orange Cherry Lemon

$ echo "${Fruits[@]:1:2}"
Orange Cherry

Dictionary

Defining Dictionary

$ declare -A sounds

$ sounds[dog]="bark"

$ sounds[cow]="moo"

$ sounds[wolf]="howl"

Lenght of Dict

$ echo "${#sounds[@]}"
3

Add / Remove Element

$ sounds[bird]="tweet"

$ echo "${#sounds[@]}"
4

$ echo "${sounds[@]}"
bark howl moo tweet

$ unset sounds[bird]

$ echo "${#sounds[@]}"
3

$ echo "${!sounds[@]}"
dog wolf cow

Iteration

  • Over key
     $ for i in  "${!sounds[@]}"; do echo "${i}"; done
     dog
     wolf
     cow
    
  • Over value
     $ for i in  "${sounds[@]}"; do echo "${i}"; done
     bark
     howl
     moo
    
Tags: bash