Skip to content

Instantly share code, notes, and snippets.

@bertrandmartel
Created August 18, 2015 00:48
Show Gist options
  • Save bertrandmartel/ff760be46960efc281dc to your computer and use it in GitHub Desktop.
Save bertrandmartel/ff760be46960efc281dc to your computer and use it in GitHub Desktop.
[ LINUX BASH ] Associate map : get / set / insert
#!/bin/bash
declare -A map=([toto]=1 [tata]=2)
YOUR_STRING="titi"
CHECK=`echo "${!map[@]}" | grep -o $YOUR_STRING`
if [ -z $CHECK ]; then
#create the entry for it and set its count (value) to 1
map[$YOUR_STRING]=0
else
# increment the count (value) by 1
var="${map[$YOUR_STRING]}"
map[$YOUR_STRING]=$((var+1))
fi
echo "your key : $YOUR_STRING | your value : ${map[$YOUR_STRING]}"
# A quick solution => (( ++map["$YOUR_STRING"] ))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment