Storing a function inside a bash array element -
how can define function element of array?
i know can this:
function array[5](){ echo "you inside function"; }
and valid function, in sense that, instruction array[5]
, correctly execute function.
unlucky, function not listed in keys of array, so, echo ${!array[*]}
not return 5
key.
for instance, following code:
array[0]="first" function array[5](){ echo "you inside function"; } array[7]="seventh" echo ${!array[*]}
only returns 0 7
.
so, how can add function array element, can loop on it?
you can't put function in array. can put function name in field. look:
a_pretty_function() { echo "you're inside function"; } array[5]=a_pretty_function # execute it: "${array[5]}"
when you're doing
array[5]() { echo "you inside function"; }
you're defining function called array[5]
can check declare -pf array[5]
.
with mechanism can horribly ugly:
array[5]() { echo "you inside function"; } i=5 "array[$i]"
and execute function array[5]
. wouldn't recommend such practice in case.
Comments
Post a Comment