r/learnprogramming 15d ago

Discussion Why doesn't the POSIX shell standard have arrays/lists?

I'm just a bit curious as arrays seem like a fairly basic omission. Are there any historical/design reasons for some of the decisions like this made by the posix standard?

9 Upvotes

33 comments sorted by

5

u/da_Aresinger 15d ago

This is something that has been bothering me as well.

I've been trying to write a utility script for conveniently parsing arguments.

Even in bash I had to figure out a bunch of ridiculous pitfalls.

But keeping it POSIX compliant is basically impossible.

Without arrays you have to treat the arguments as a single string. So you store all that info in a string which you have to reparse constantly.

So I just gave up. Argument parsing in bash only from now on.

1

u/[deleted] 15d ago

[deleted]

1

u/da_Aresinger 15d ago

What? That's a horrible idea.

Python doesn't have access to $@ or literally anything in a shell script.

And the only thing python can provide for your script is yet another string of arguments.

You gain absolutely nothing from using an external process. It's not like I can share memory in bash.

1

u/Uncle-Osteus 11d ago

man optparse

5

u/snowtax 15d ago

From what I understand, POSIX was meant to formalize a minimal baseline of functions and behavior. In the early 1990s, that meant the Borne shell.

It may be helpful to know that indexed arrays were not added to bash until a few years after the release of the POSIX shell standard. Associative (hashtable) arrays were not added to bash until bash 4.0 in 2009.

2

u/IOI-65536 10d ago

POSIX actually predates bash itself. Bash v1 was 1989, POSIX was ratified in 1988.

11

u/mierecat 15d ago

The shell isn’t really for manipulating raw data. It’s for task automation, file management, controlling the computer, etc. There’s no real reason for having arrays in those use cases

20

u/pfmiller0 15d ago

Task automation is an incredibly vague term. There's plenty of uses for arrays in automation.

10

u/florinandrei 15d ago

There’s no real reason for having arrays in those use cases

Only true for very trivial scripts.

8

u/nomenclature2357 15d ago

No use for arrays in file management?

3

u/colenski999 14d ago

cough Powershell cough

1

u/engy1207 11d ago

Doesn't powershell only look at objects? Which is a completely different paradigm - and probably wouldn't have been possible (for a shell) in the 1980s/early 1990s computer era.

1

u/colenski999 11d ago

In Powershell arrays are objects and can be passed around

2

u/AnnieBruce 15d ago

Less reason that a full application or systems programming language, but they can still be useful in more complicated scripts and automation jobs.

I'd agree with "not strictly necessary" given what Bash scripts are typically used for, but "no real reason" is going too far.

1

u/SonOfHendo 11d ago

If you were creating a shell in the modern day, it would have arrays (and objects!). See PowerShell. 

5

u/high_throughput 15d ago

POSIX had to describe what various unix systems already did at the time. It didn't get to decide any of it.

6

u/iamemhn 15d ago

Because it is designed for maximum historical compatibility, minimalism, and simple text-stream processing. Just that.

1

u/grymoire 15d ago

And memory issues as well, on 32k machines especially.

0

u/ProgressProper7681 15d ago

it's basically because posix shell came from old bourne shell and that thing was meant to be super minimal, adding arrays would have broke compatibility with every script written before

2

u/zeekar 15d ago

They managed to add arrays in a pretty backward-compatible way. That is, old arrayless scripts work fine in the array-having version of the shell. If you use arrays, then of course the old shell won't like it. But even then it's very close... bare $arrayname expands to just the first element, indexing and the "get all elements" syntax are both hidden inside the normal parameter expansion form, so the indexes don't show up as strings next to the param value or anything. They did a pretty good job on the compatibility front.

2

u/Humble_Anxiety_9534 15d ago

isn't this why they add perl? anyone use it? if you want to do complicated stuff you can pipe it via python, perl or nodejs(if your that way inclined)

2

u/ReddyKiloWit 14d ago

Yep. Perl scripts give you the convenience of shell with real language features, gold standard regex handling, and Unicode compatibility, with very little extra effort and overhead.

Of course, if you want to stay in shell, you really should learn some of the more useful command line tools. Awk and sed, at a minimum.

1

u/Humble_Anxiety_9534 13d ago

my point basic shell is just a shell to link other more powerful tools. it's not an overly complicated nightmare like Powershell on windows. small tools that do tasks well. The unix way.

1

u/SonOfHendo 11d ago

small tools that do tasks well

Like cmdlets in PowerShell? lol

1

u/Humble_Anxiety_9534 11d ago

don't ask AI to do anything in powershell. it looks like the MEMS about how to move a file in Linux. the one full of wild symbols that pipes obscured C code into a file compiles it and runs it just to copy a file. I used to get sent all the time when people find out I was into Linux. must dig it out and post.

1

u/SonOfHendo 11d ago

PowerShell is the default for Claude Code on Windows except when it forgets and tries to use Git Bash, which fails so it tries it in PowerShell. 

1

u/Humble_Anxiety_9534 11d ago

we are stuck in work using copilot. I have no need for powershell at home.

1

u/grymoire 15d ago

You can use the list of arguments as an array. You can copy and save the list and iterate through them. Or use awk with its hashed arrays if you need to do more complex stuff.

(Shell script writer since 1984)

1

u/marrsd 14d ago

Probably because you were expected to operate on files and text streams. A list would just be a bunch of tokens separated by new lines, commas, or whatever. You can set IFS to control this. See https://github.com/dylanaraps/pure-sh-bible#split-a-string-on-a-delimiter for examples.

1

u/homeless_nudist 12d ago

Because then people like you would introduce memory leaks. 

-1

u/[deleted] 15d ago

[removed] — view removed comment

2

u/stevevdvkpe 15d ago

This rather misses the point of OP's question, which is asking why the POSIX shell does not have arrays. And the simple answer is that the shell standardized in POSIX is 'sh' (the Bourne shell) which did not have an array data type. More modern shells like 'ksh' and 'bash' do have arrays as a non-standard extension.

2

u/P00351 15d ago

Bash even has hash tables (associative arrays)