Extracting x random values from a bash array
Decided to write an answer, as I found the --input-range
option to shuf
that turned out handy:
N=3
ARRAY=( zero one two three four five )
for index in $(shuf --input-range=0-$(( ${#ARRAY[*]} - 1 )) -n ${N})
do
echo ${ARRAY[$index]}
done
How about this:
for i in {1..10}; do
echo $i
done | shuf
That will return all the numbers. If you only want a specific amount, do this:
numbers=5
for i in {1..10}; do
echo $i
done | shuf | head -$numbers
And if you want to change the numbers, just change the {1..10}
variable to whatever you want.