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 " "
}