Arrays in ash shell?


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.


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.

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

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

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