Live data from Hacker News

Today I learned that bash has hashmaps (2024)

xeiaso.net

111–120 of 137 posts

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

#111
post #57

Earlier quoted context omitted.

They suggest that based on their experience. Error handling in bash is awful so I advice against using it for anything complex.

Anything complex should be written in a competent language like Java. Script languages (like Bash and Python) are for short (a few lines long) scripts. Using the tool outside the scope of what it was designed for is not a good idea.

Using a tool beyond its design can be problematic.

But Python is not designed to only be a scripting language:

> What is Python?

> Python is an interpreted, interactive, object-oriented programming language. It incorporates modules, exceptions, dynamic typing, very high level dynamic data types, and classes. It supports multiple programming paradigms beyond object-oriented programming, such as procedural and functional programming. Python combines remarkable power with very clear syntax. It has interfaces to many system calls and libraries, as well as to various window systems, and is extensible in C or C++. It is also usable as an extension language for applications that need a programmable interface. Finally, Python is portable: it runs on many Unix variants including Linux and macOS, and on Windows.

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

#112
post #95
post #57

Earlier quoted context omitted.

They suggest that based on their experience. Error handling in bash is awful so I advice against using it for anything complex.

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.

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

#113
post #107

Earlier quoted context omitted.

From man(1) less: &pattern Display only lines which match the pattern; lines which do not match the pattern are not displayed. If pattern is empty (if you type & immediately followed by ENTER), any filtering is turned off, and all lines are displayed. While filtering is in effect, an ampersand is displayed at the beginning of the prompt, as a reminder that some lines in the file may be hidden. $ less --version less 4…

Oh, you meant the program “less”! Right, I understand what you meant now.

:-)

Yeah.

I was viewing some files and thinking "it'd be really cool if less(1) had some way of filtering to just lines matching a specific pattern".

RTFM'd and TIL'd.

Mind that at that point I'd used less for ... oh, at least 30 years.

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

#114
post #72

Earlier quoted context omitted.

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

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

Evolve into what? Something that works and doesn't have foot guns? Then you might as well just use any other programming languages that exist today. I will not suggest any alternative as that just attract flame wars.

Why hasn't using a rock as a tool evolved? Exactly, because it's god-awful and got superseded, use a real hammer or a cordless screwdriver instead.

Just like the post about improvements to C on the front page today, that list can be made infinite. Language designers have already learned from those mistakes and created zig, rust, go or even c++, that fixes all of them and more. Fixing all the flaws will turn it into a different language anyhow. There is only that much you can polish a turd.

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

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

I do a fair bit of bash scripting and there is no way I can remember all the features and syntax I use, so of course I read parts of the manual regularly and often learn new things. You don't need to ever read the whole thing.

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

#116

Nice that these exist—but does anyone else absolutely abhor shell programming? The syntax is impossible to memorize, it’s incredibly easy to make mistakes, and debugging is a pain. I hate it more than C++ and AppleScript.

It has some rough edges but bash has the world's best REPL and along with -x that makes debugging quite easy.

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

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

You have this the wrong way around. Associative array is the abstraction you are talking about. Hash maps are a specific implementation of that abstraction. You could also define hash map as an abstraction which has the same interface as associative array but with more strict performance characteristics. But either way it's wrong because bash is neither implemented as a hash map nor has the performance characteristics of a hash map.

For reference the main implementations of associative array are alists/plists (linear time), balanced binary trees, b-trees (logarithmic time) and hash maps (amortised constant time).

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

#119

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

"fantastic foot guns" It wouldn't be a worthy bash feature if after learning about it you wouldn't spend a few days figuring out why the damn thing doesn't work the way it works in any other language.

Indeed! It's delightful like all of bash. I've used them in a few places but I try not to

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

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

I once worked with a dev (frontend, of all things) who reread the bash manual annually.

I work in ops, and I don’t even have that dedication. My hat is off to them.

Post reply on HN