3

The ability to use arrays in bash and ksh93 is very convenient when the need arises to quickly access elements from multiple arrays. Here is a quick example:

#!/bin/ksh93 --

array1=(1 2 3 4 5 6 7)

array2=(8 9 10 11 12 13 14)

printf '%s\n' "Third element of array1 is ${array1[2]} and fifth element of array2 is ${array2[4]}"

I remember reading in passing that at least part of the reason that arrays are not included in the POSIX standard is that doing so would break things. Is this true? What things would break?

1

1 Answer 1

2

I don't know what text you're referring to, but I'm pretty sure this is not the case, since bash and ksh93 are supposed to be POSIX compliant, and if arrays did cause POSIX shells to break, then those shells would not comply.

However, that doesn't mean that POSIX is likely to include them. First, bash and ksh93 may not implement arrays the same way, and POSIX would not want to specify a feature that behaved differently in widely deployed historical implementations.

Second, POSIX generally doesn't specify a lot of features. Only now is there even a proposal to require local, which is widely deployed in almost every open source POSIX shell except ksh93. Similarly, it would be possible to use a vi implementation that implemented only the POSIX features, but most users would find it quite sparse on features.

Finally, POSIX prefers to standardize things which are already available in most implementations, and shell arrays are not such a thing. dash lacks them, as do most of the shells with which it shares lineage, as well as tiny implementations such as busybox. A complete POSIX environment can be relatively tiny, and that's very convenient for people who need to work with embedded systems, so nice-to-haves are often omitted in favor of simplicity.

Note also that just because widely deployed open source implementation have a feature doesn't mean that commercial Unix environments will. Even if POSIX were to specify arrays in a new version, it would be many years before all of those systems were updated to support them.

2
  • While local or any form of scoping is widely deployed. How it's done varies greatly between implementations, which is why it makes it very difficult to standardise it. See List of shells that support `local` keyword for defining local variables Commented Aug 18, 2020 at 7:01
  • I agree that standardizing local won't be trivial, but there is an issue filed with the Austin Group to get it added, and at least most of the shells in Debian seem to implement it in a useful enough way that Debian can require it for all /bin/sh implementations.
    – bk2204
    Commented Aug 20, 2020 at 1:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .