How to use pseudo-arrays in POSIX shell script?

The basic idea is to use set to re-create the experience of working with indexed values from an array. So when you want to work with an array, you instead run set with the values; that’s

set -- 1895 955 1104 691 1131 660 1145 570 1199 381

Then you can use $1, $2, for etc. to work with the given values.

All that’s not much use if you need multiple arrays though. That’s where the save and eval trick comes in: Rich’s save function¹ processes the current positional parameters and outputs a string, with appropriate quoting, which can then be used with eval to restore the stored values. Thus you run

coords=$(save "$@")

to save the current working array into coords, then create a new array, work with that, and when you need to work with coords again, you eval it:

eval "set -- $coords"

To understand the example you have to consider that you’re working with two arrays here, the one with values set previously, and which you store in coords, and the array containing 1895, 955 etc. The snippet itself doesn’t make all that much sense on its own, you’d have some processing between the set and eval lines. If you need to return to the 1895, 955 array later, you’d save that first before restoring coords:

newarray=$(save "$@")
eval "set -- $coords"

That way you can restore $newarray later.


¹ Defined as

save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}


The idea is to encode the list of arbitrary strings into a scalar variable in a format that can later be used to reconstruct the list or arbitrary strings.

 $ save_pseudo_array x "y z" $'x\ny' "a'b"
'x' \
'y z' \
'x
y' \
'a'\''b' \

$

When you stick set -- in front of that, it makes shell code that reconstructs that list of x, y z strings and stores it in the $@ array, which you just need to evaluate.

The sed takes care of properly quoting each string (adds ' at the beginning of the first line, at the end of the last line and replaces all 's with '\'').

However, that means running one printf and sed command for each argument, so it's pretty inefficient. That could be done in a more straightforward way with just one awk invocation:

save_pseudo_array() {
  LC_ALL=C awk -v q=\' '
    BEGIN{
      for (i=1; i<ARGC; i++) {
        gsub(q, q "\\" q q, ARGV[i])
        printf "%s ", q ARGV[i] q
      }
      print ""
    }' "$@"
}