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" key -string "$value"
...where $dictionary
is a variable, but this is not working.
Is there a way to create key-value pairs in Bash script?
In bash version 4 associative arrays were introduced.
The arr array now contains the three key value pairs. Bash is fairly limited what you can do with them though, no sorting or popping etc.
Will loop over all key values and echo them out.
If you can use a simple delimiter, a very simple oneliner is this:
Hereby
i
is filled with character sequences like"a,b"
and"c_s,d"
. each separated by spaces. After thedo
we use parameter substitution to extract the part before the comma,
and the part after it.For persistent key/value storage, you can use
kv-bash
, a pure bash implementation of key/value database available at https://github.com/damphat/kv-bashUsage
Try create some permanent variables