arraysbashposixdash-shell

Arrays in a POSIX compliant shell


According to this reference sheet on hyperpolyglot.org, the following syntax can be used to set an array.

i=(1 2 3)

But I get an error with dash which is the default for /bin/sh on Ubuntu and should be POSIX compliant.

# Trying the syntax with dash in my terminal
> dash -i
$ i=(1 2 3)
dash: 1: Syntax error: "(" unexpected
$ exit

# Working fine with bash
> bash -i
$ i=(1 2 3)
$ echo ${i[@]}
1 2 3
$ exit

Is the reference sheet misleading or erroneous?
If yes, what would be the correct way to define an array or a list and be POSIX compliant?


Solution

  • Posix does not specify arrays, so if you are restricted to Posix shell features, you cannot use arrays.

    I'm afraid your reference is mistaken. Sadly, not everything you find on the internet is correct.