Arrays in a POSIX compliant shell

As said by rici, dash doesn't have array support. However, there are workarounds if what you're looking to do is write a loop.

For loop won't do arrays, but you can do the splitting using a while loop + the read builtin. Since the dash read builtin also doesn't support delimiters, you would have to work around that too.

Here's a sample script:

myArray="a b c d"

echo "$myArray" | tr ' ' '\n' | while read item; do
  # use '$item'
  echo $item
done

Some deeper explanation on that:

  • The tr ' ' '\n' will let you do a single-character replace where you remove the spaces & add newlines - which are the default delim for the read builtin.

  • read will exit with a failing exit code when it detects that stdin has been closed - which would be when your input has been fully processed.

  • Since echo prints an extra newline after its input, that will let you process the last "element" in your array.

This would be equivalent to the bash code:

myArray=(a b c d)

for item in ${myArray[@]}; do
  echo $item
done

If you want to retrieve the n-th element (let's say 2-th for the purpose of the example):

myArray="a b c d"

echo $myArray | cut -d\  -f2 # change -f2 to -fn

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.