Live data from Hacker News

Today I learned that bash has hashmaps (2024)

xeiaso.net

121–130 of 137 posts

Re: Today I learned that bash has hashmaps (2024)

#121
post #25
post #23

Earlier quoted context omitted.

While I agree with the larger sentiment I think you’re making (I also make it a habit to peruse the manual just to see what is available), how often do you reread the manual, especially for your shell? I knew about associative arrays in bash, but not by reading the manual as those were introduced in v4 and I’ve been using bash for longer than that.

You should probably read the release notes for new major versions. Also, anytime you think “ I wish there was available in this program ”, you should probably double-check if such a feature has appeared since you last learned the program.

Agreed. This is also how I found out that features in Python I took for granted, like breakpoint(), were much more recent than I thought (3.7). Nothing like having to use ancient versions of a language to discover limitations.

Re: Today I learned that bash has hashmaps (2024)

#122
post #95

Earlier quoted context omitted.

You can go a really really long way, with a script that will work everywhere, with just set -e and aborting at the first error. Better yet, 'bash unofficial strict mode': set -euo pipefail IFS=$'\n\t'

That has lots of issues. See https://mywiki.wooledge.org/BashPitfalls#set_-euo_pipefail for some.

It does, yes, but IME if you can get people who aren’t familiar with shell to use it, it prevents more problems than it solves on the whole.

Then again, so does enforcing passing shellcheck in CI.

Re: Today I learned that bash has hashmaps (2024)

#123
post #77
post #75

Earlier quoted context omitted.

Which is because they're not hashmaps, they're associative arrays. Article treats them as the same thing, but they're not - that's why the "declare -A" is A and not H - it stands for "associative".

Author of the article here. As far as I care, if it quacks like a hashmap, it's better to describe it as a hashmap. The fact that it's an associative array under the hood is irrelevant.

It sounds like you don't even know what these words mean.

Re: Today I learned that bash has hashmaps (2024)

#124
post #72

Earlier quoted context omitted.

Why hasn't shell scripting evolved? It's god-awful.

- bash is available pretty much everywhere, so if you learn it you can always use it, whereas if you learn a weird new shell you'll be occasionally forced to fall back on bash anyway, so people learn just bash for efficiency's sake (because learning one shell is painful enough as it is). And any proposed replacement will be non-portable. - some of the things that make shell scripting terrible can't be fixed in the sh…

> it would be awesome if every Unix utility output structured data like JSON

I see this argument a lot, and I think it has a ton of overlap with the struggles from devs trying to grok RDBMS I see as a DBRE.

Most (?) people working with web apps have become accustomed to JSON, and fully embrace its nesting capabilities. It’s remarkably convenient to be able to deeply nest attributes. RDBMS, of course, are historically flat. SQL99 added fixed-size, single-depth arrays, and SQL2003 expanded that to include arbitrary nesting and size; SQL2017 added JSON. Still, the traditional (and IMO, correct) way to use RDBMS is to treat data as having relationships to other data, and to structure it accordingly. It’s challenging to do, especially when the DB providers have native JSON types available, but the reasons why you should are numerous (referential integrity, size efficiency, performance…).

Unix tooling is designed with plaintext output in mind because it’s simple, every other tool in the ecosystem understands it, and authors can rest assured that future tooling in the same ecosystem will also understand it. It’s a standard.

JSON is of course also a standard, but I would argue that on a pure CLI basis, the tooling supporting JSON as a first-class citizen (jq) is far more abstruse than, say, sed or awk. To be fair, a lot of that is probably due to the former’s functional programming paradigm, which is foreign to many.

Personally, I’m a fan of plaintext, flat output simply because it makes it extremely easy to parse with existing tooling. I don’t want to have to fire up Python to do some simple data manipulation, I want to pipe output.

Re: Today I learned that bash has hashmaps (2024)

#125
Bash Associative Arrays [1] are handy! Some examples of how I've used them:

- my site builder (for evalapply.org): inject metadata into page templates. e.g. https://github.com/adityaathalye/shite/blob/b4163b566f0708fd...

- oxo game (tic-tac-toe): reverse index lookup table for board positions: https://github.com/adityaathalye/oxo/blob/7681e75edaeec5aa1f...

- personal machine setup: associate name of installed application to its apt source name, so we can check for the app, and then install package https://github.com/adityaathalye/bash-toolkit/blob/f856edd30...

[1] I'd say "hashmap" is acceptable, colloquially. However, I don't think Bash computes hashes of the keys.

(edit: fix formatting snafu)

Re: Today I learned that bash has hashmaps (2024)

#126

Earlier quoted context omitted.

- bash is available pretty much everywhere, so if you learn it you can always use it, whereas if you learn a weird new shell you'll be occasionally forced to fall back on bash anyway, so people learn just bash for efficiency's sake (because learning one shell is painful enough as it is). And any proposed replacement will be non-portable. - some of the things that make shell scripting terrible can't be fixed in the sh…

> it would be awesome if every Unix utility output structured data like JSON I see this argument a lot, and I think it has a ton of overlap with the struggles from devs trying to grok RDBMS I see as a DBRE. Most (?) people working with web apps have become accustomed to JSON, and fully embrace its nesting capabilities. It’s remarkably convenient to be able to deeply nest attributes. RDBMS, of course, are historically…

If there were some kind of standard or widely-followed convention for Unix tools to print plaintext, I wouldn't mind it so much. It's the fact that you need to memorize a different flag and output format for each tool, followed by an awk command where the intention of the code is generally very obtuse, which bothers me. By contrast, for all its faults, the fact that PowerShell has unambiguous syntax for "select this field from the output" helps a lot with both reading and writing. e.g. to get your IP address, "Get-NetIPAddress | ? {$_.InterfaceAlias -like "Ethernet" -or $_.InterfaceAlias -like "Wi-Fi"} | select IPAddress" is a lot clearer in intent than the Unix equivalent regex soup, and it can be written without looking anything up by printing the raw output of "Get-NetIPAddress" to the shell and seeing what you need to filter/select on. You can even get tab completion to help.

A hypothetical Unix equivalent doesn't need to be JSON, I just brought that up as an example. But any structured data would be an improvement over the situation now, and as the PS example shows, with the appropriate ecosystem tooling you can do all the data manipulation over pipes.

Re: Today I learned that bash has hashmaps (2024)

#127
post #42

One thing the article doesn't mention is how you can use indirect expansion to get a list of all keys. For example: `${!myvar[@]}` would list all of the keys. I've written about associative arrays in Bash a bit here: https://nickjanetakis.com/blog/associative-arrays-in-bash-ak...

doesn't work in zsh?

It doesn't but if you're putting this into a script that's ok. You can set the script's shebang to bash so even if your user's shell is using zsh, the script will run with bash.

Re: Today I learned that bash has hashmaps (2024)

#128
post #94
post #38

Earlier quoted context omitted.

I think many devs don't know the difference and simply call any dictionary/associative array a hash map. It might be one of the concepts that "the internet" promotes: it sounds fancy, more technical, makes you seem more knowledgeable, so it gets repeated. Then newcomers think this is the way it's always been called, and that gives it enough visibility to become the preferred name.

Quite frankly I'd love a programmers dictionary over terms, I recently called a file with a csv like format a db because, well, its not a csv and I don't know what else to call it.

A table?

Re: Today I learned that bash has hashmaps (2024)

#129
post #128
post #94

Earlier quoted context omitted.

Quite frankly I'd love a programmers dictionary over terms, I recently called a file with a csv like format a db because, well, its not a csv and I don't know what else to call it.

A table?

well, yeah, that's what a csv is but the format is not regular.

Re: Today I learned that bash has hashmaps (2024)

#130
post #48

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

Too many. I still write /bin/sh syntax (I know it's a symlink to bash now but I mean the old school sh). Anything that requires bash-that-isnt-sh is usually better written in perl or something else.

Only on some distro's.

Debian variants tend to link dash, which self consciously limits itself to posix compatibility.

Post reply on HN