Is there a way to create key-value pairs in Bash script?

后端 未结 5 2092
不思量自难忘°
不思量自难忘° 2020-12-22 19:05

I am trying to create a dictionary of key value pair using Bash script. I am trying using this logic:

declare -d dictionary
defaults write \"$dictionary\" ke         


        
5条回答
  •  甜味超标
    2020-12-22 19:20

    In bash, we use

    declare -A name_of_dictonary_variable
    

    so that Bash understands it is a dictionary.

    For e.g. you want to create sounds dictionary then,

    declare -A sounds
    
    sounds[dog]="Bark"
    
    sounds[wolf]="Howl"
    

    where dog and wolf are "keys", and Bark and Howl are "values".

    You can access all values using : echo ${sounds[@]} OR echo ${sounds[*]}

    You can access all keys only using: echo ${!sounds[@]}

    And if you want any value for a particular key, you can use:

    ${sounds[dog]}

    this will give you value (Bark) for key (Dog).

提交回复
热议问题