Skip to content(if available)orjump to list(if available)

Today I learned that bash has hashmaps (2024)

teddyh

It’s amazing how much of a superpower merely reading the manual is nowadays.

<https://www.gnu.org/software/bash/manual/bash.html#Arrays>

somat

I love shell, I think it's killer feature are pipes and whoever figured out how to design so you can pipe in and out of control structures(Doug Mcilroy?) is a goddamn genius. however, after writing one to many overly clever shell scripts, I have a very clearly delineated point in which the script has become too complex and it is time to rewrite in a language better suited for the task. and that point is when I need associative arrays.

A lot of the sins of shell are due to it's primary focus as an interactive language. Many of those features that make it so nice interactively really hurt it as a scripting language.

aunderscored

Not only do they exist, but they have some fantastic foot guns! https://mywiki.wooledge.org/BashPitfalls#A.5B.5B_-v_hash.5B....

forgotpwd16

>they have some fantastic foot guns!

Otherwise wouldn't be getting the full shell experience.

stefankuehnel

Interesting, definitely need to keep that in mind.

dghf

Picky and probably pointless question: are they actually hashmaps? If I understand correctly, a hashmap isn’t the only way to implement an associative array.

biorach

Every few years I rediscover this fact and every few years I do my best to forget it

agnishom

> Q: How do I declare a Hashmap?

> A: You use the command `declare -A HASHMAP_NAME`

This is why I think Bash is a horrible language

jolmg

Because of the -A instead of an -H? -A is for "Associative array".

arter4

what do you mean?

j45

For some tasks, if as much as possible was coded in bash, it would work being called anywhere from any programming language.

Now to add hashtables to that.

oniony

A couple of weeks ago I learnt that Bash on Mac does not have associative arrays. We worked around the issue by changing the script to run under Zsh, but beware.

teddyh

Many programs on macOS are stuck in the ancient past, due to Apple:

<https://web.archive.org/web/20240810094701/https://meta.ath0...>

ptdorf

Sounds like a bash 3 issue.

$ bash --version

GNU bash, version 5.2.32(1)-release (aarch64-apple-darwin23.4.0)

$ declare -A aaa; aaa[a]=a; aaa[b]=bb; for i in ${!aaa[@]}; do echo "$i --> ${aaa[$i]}"; done

b --> bb

a --> a

forgotpwd16

To elaborate on this, macOS default bash is still stuck (assuming due to licensing) in v3.2.x (released in 2007). Bash got associative arrays in v4 (released in 2009).

x3n0ph3n3

You should be getting bash from homebrew anyways.

pletnes

Just use the included zsh.

crabbone

To me, this is a development in the wrong direction. Shell is great precisely because it's so minimal. Everything is string rule is one that calms all of your type-induced fears.

Having to implement hash-tables, while still keeping the appearances of everything being a string is the anti-pattern known as "string programming" (i.e. when application develops a convention about storing type / structure information inside unstructured data, often strings).

I would love for there to be a different system shell with a small number of built-in types that include some type for constructing complex types. There are many languages that have that kind of type system: Erlang, for example. But, extending Unix Shell in this way is not going to accomplish this.