function to get a random element of an array bash
arrays, bash, random
Solution
Change your function to:
randArrayElement(){ arr=("${!1}"); echo ${arr["$[RANDOM % ${#arr[@]}]"]}; }
and call it as:
randArrayElement "ARRAYISO[@]"
Problem
I know how to get a random item from an array like that: ``` declare -a ARRAYISO=(100 200 400 800) echo ${ARRAYISO["$[RANDOM % ${#ARRAYISO[@]}]"]} ``` I could obviously do that for each array, like a donkey, but I'd like to make a function which takes an array as argument and returns a random element. I'm trying with: ``` randArrayElement() { randElement=${$1["$[RANDOM % ${#$1[@]}]"]} echo $randElement } randArrayElement ARRAYISO ``` but it doesn't like my $1... I've tryed with ", ', ` , bash doesn't interpret the $1 var...