⇐ Previous | Next ⇒

Create a Hash-like structure with Bash

Post created 2013-12-17 13:55 by Gabe Koss.



This is pretty cool and worth sharing. In Bash you can use declare -A VarName to create a Hash-like structure which you can use to store key / value pairs in.

Lets take a look:

$ declare -A CapitalCity $ CapitalCity[France]="Paris" $ echo "${CapitalCity['France']}" Paris

Pretty damn cool. I discovered this trick in Jason Brittains 'Bash as a Modern Programming Language' presentation. Slides are available.

Enumerate set values from declare

To get a list of set values you can run declare -A and it will print a list similar to the following:

$ declare -A declare -A BASH_ALIASES='()' declare -A BASH_CMDS='()' declare -A CapitalCity='([France]="Paris" )' declare -A _xspecs='(... <many many> ...)'

Please enable JavaScript to view the comments powered by Disqus.

Disqus