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

153,635

Solution 1

In bash version 4 associative arrays were introduced.

declare -A arr

arr["key1"]=val1

arr+=( ["key2"]=val2 ["key3"]=val3 )

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.

for key in ${!arr[@]}; do
    echo ${key} ${arr[${key}]}
done

Will loop over all key values and echo them out.

Note: Bash 4 does not come with Mac OS X because of its GPLv3 license; you have to download and install it. For more on that see here

Solution 2

If you can use a simple delimiter, a very simple oneliner is this:

for i in a,b c_s,d ; do 
  KEY=${i%,*};
  VAL=${i#*,};
  echo $KEY" XX "$VAL;
done

Hereby i is filled with character sequences like "a,b" and "c_s,d". each separated by spaces. After the do we use parameter substitution to extract the part before the comma , and the part after it.

Solution 3

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).

Solution 4

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-bash

Usage

git clone https://github.com/damphat/kv-bash
source kv-bash/kv-bash

Try create some permanent variables

kvset myName  xyz
kvset myEmail [email protected]

#read the varible
kvget myEmail

#you can also use in another script with $(kvget keyname)
echo $(kvget myEmail)

Solution 5

Using an example above this is what I did

#!/bin/sh
lookup_vht_index() {
for i in VHT20,0 VHT40,1 VHT80,2 VHT160,3 ; do
  KEY=${i%,*};
  VAL=${i#*,};
#  echo $KEY" XX "$VAL;
  [ "$1" = "$KEY" ] && echo $VAL
done
}

lookup_vht_name() {
for i in 0,VHT20 1,VHT40 2,VHT80 3,VHT160 ; do
  KEY=${i%,*};
  VAL=${i#*,};
#  echo $KEY" XX "$VAL;
  [ "$1" = "$KEY" ] && echo $VAL
done
}


echo "VHT20="$(lookup_vht_index "VHT20")
echo "2="$(lookup_vht_name 2)
Share:
153,635

Related videos on Youtube

RKS
Author by

RKS

Updated on February 17, 2022

Comments

  • RKS
    RKS over 2 years

    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?

    • RKS
      RKS over 11 years
      i was working on bash. Figured a way to do this myself.
    • RKS
      RKS over 11 years
      use of this also help: urls+=( '<dict><key>key1</key><string>'$value1'</string><key>key2</k‌​ey><string>'$value2'‌​</string><key>key3</‌​key><string>'$value3‌​'</string></dict>'
    • johnsyweb
      johnsyweb over 11 years
      Great! You're allowed (and even encouraged) to answer your own questions on StackOverflow, that way you'll help others in a similar situation.
    • shellter
      shellter over 11 years
      I will upvote your answer if you include some sample usage and output. Good luck.
  • PleaseStand
    PleaseStand over 11 years
    It is important to note that Bash 4 does not come with Mac OS X because of its GPLv3 license; you have to download and install it. (Apple still ships Bash 3.2.)
  • AlikElzin-kilaka
    AlikElzin-kilaka almost 8 years
    Can you please explain the example you gave?
  • AlikElzin-kilaka
    AlikElzin-kilaka almost 8 years
    There's a con here: The iteration is not ordered by insertion order.
  • AlikElzin-kilaka
    AlikElzin-kilaka almost 8 years
    Question: Will it work if b is a variable containing spaces?
  • math
    math almost 8 years
    in my example, b is not a variable, and no it will not work, as the list for the for-loop is space separated.
  • Mamun
    Mamun over 4 years
    definitely needs bash version 4. Otherwise declare -A does not work. Mac comes with bash 3.2
  • FullStack Alex
    FullStack Alex about 4 years
    It's not because of the GPLv3 license but because Apple doesn't want to use GPLv3 licensed software. So it's nothing wrong with GPLv3 license but something wrong with Apple, I assume.