Live data from Hacker News

Today I learned that bash has hashmaps (2024)

xeiaso.net

21–30 of 137 posts

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

#21

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

>they have some fantastic foot guns! Otherwise wouldn't be getting the full shell experience.

It's not like we would have 50 years of good programming language design and then stick to things that were created 50 years ago.

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

#22
post #18

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 a…

Pipe-related concepts in various restricted forms were floating around for years. Doug McIlroy indeed proposed them in 1964 and was heading the Bell Labs team when they were implemented in the Third Research Edition of Unix (1973).

See https://youtu.be/FbDebSinSQo?si=xbWIqES80hI2S802&t=901

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

#23
post #13

It’s amazing how much of a superpower merely reading the manual is nowadays. https://www.gnu.org/software/bash/manual/bash.html#Arrays >

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.

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

#24
I advocate the following rules for when to write and when not to write a shell script.

# Write a shell script:

* Heavy lifting done by powerful tool (sort, grep, curl, git, sed, find, …)

* Script will glue diverse tools

* Workflow resembles a pipeline

* Steps can be interactively developed as shell commands

* Portability

* Avoid dependency hell

* One-off job

# Avoid shell scripting:

* Difficult to see the preceding patterns

* Hot loops

* Complex arithmetic / data structures / parameters / error handling

* Mostly binary data

* Large code body (> 500 LoC)

* Need a user interface

A need for associative arrays (implemented in Bash as via hashmaps) moves the task to the second category (avoid shell scripting).

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

#25
post #23
post #13

It’s amazing how much of a superpower merely reading the manual is nowadays. https://www.gnu.org/software/bash/manual/bash.html#Arrays >

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.

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

#26
post #13

It’s amazing how much of a superpower merely reading the manual is nowadays. https://www.gnu.org/software/bash/manual/bash.html#Arrays >

TBF, the Bash manpage now runs > 80 pages:

  man -Tps bash | ps2pdf - bash.pdf
That can be daunting. Though illuminating.

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

#27

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 insid…

> To me, this is a development in the wrong direction.

That ship has sailed. These were introduced in Bash 4, released in 2009, and Bash is already on version 5.

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

#28
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.

That's how I discovered the '&' regex feature in less some years back.

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

#29
post #18

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 a…

Pipe-related concepts in various restricted forms were floating around for years. Doug McIlroy indeed proposed them in 1964 and was heading the Bell Labs team when they were implemented in the Third Research Edition of Unix (1973). See https://youtu.be/FbDebSinSQo?si=xbWIqES80hI2S802&t=901

Sure, but in and out of control structures? I think that's the major point your parent was making.

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

#30
I'm guilty of this. I knew zsh had them but since I can never remember the exact things zsh has that bash doesn't, I just assume anything remotely useful isn't compatible.

This policy comes from a six hour debugging session involving (somewhere) a script that manipulated a binary file - bash can't store zero bytes in variables and zsh can, but it's not like it'll tell you that it's pointlessly truncating your data and I never thought to look. So now every step in that script gets converted back and forth through xxd, and I don't trust Bash anymore.

Post reply on HN