Arrays in ash shell?

Hi,

I want to use the functionality of an array in the ash shell. I read in an old post that it's not supported in this version of ash, but is there some trick I can use to mimic that functionality? I just want to append some values to the array, or I guess I could just conditionally concatenate a string.

AFAIK, the openwrt default ash shell provided by busybox does not support arrays like that supported by bash. The default separator is a space but you can work around that using the IFS (Internal Field Separator). String concatenation should work ok. I can share a simple function to "search" a string array for an item if you like...

Alternatively, you can install bash or look into awk (also available via busybox by default with openwrt) which does support array's (and hash like array's). I find using awk a bit of pita tho.

HTH

EDIT: in the past, if you could count on luci being installed, you can use lua. I'm not sure that will be the case in the future tho as luci seems to be moving to javascript.

2 Likes
# ash+eval
for I in $(seq 10)
do
    eval "A${I}=$((I**2))"
    eval "echo \${A${I}}"
done

# awk
awk -f - << EOF
BEGIN {
    for(i=1; i<=10; i++) {
        a[i] = i^2
        print a[i]
    }
}
EOF
2 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.