Cancel

Dictionary/Objects in Bash

Dictionary is the collection of key and value pair which helps you to improve your productivity. You can create an dictionary/object like the following code, values can be either wrapped with single/double quotes, but keys must wrapped with square brackets, not quotes.

Run the following code to create dictionary/object in bash.

1
2
3
4
5
declare -A channel=(
  [name]="Yafiz Abraham"
  [course]="Bash Scripting"
  [platform]="YouTube"
)

To return all values.

1
echo ${channel[@]}

To return all keys.

1
echo ${!channel[@]}

To return total number of items, or length of dictionary/object.

1
echo ${#channel[@]}

To return single item via specific key.

1
echo ${channel[platform]}

To insert/push new item at the end of dictionary/object.

1
channel[blog]+="deletify.app"

To update item by the specific key from dictionary/object.

1
channel[blog]="https://deletify.app"

To remove item by the specific key from dictionary/object.

1
unset channel[blog]