Hi
I wanted to print random elements from an array at bash shell
I use the following code, but I always see first element getting printed
#!/bin/bash
c=1
expressions=(pink red white yellow purple)
while [[ $c -le 9 ]]; do
echo "The value of RANDOM is $RANDOM"
selectedexpression=${expressions[$RANDOM % ${#RANDOM
[*]}]};
echo "***** The option is $selectedexpression"
sleep 1
((c++))
done
The output as follows:
The value of RANDOM is 10813
***** The option is pink
The value of RANDOM is 30688
***** The option is pink
The value of RANDOM is 9752
***** The option is pink
The value of RANDOM is 2170
***** The option is pink
The value of RANDOM is 4010
***** The option is pink
The value of RANDOM is 31161
***** The option is pink
The value of RANDOM is 2046
***** The option is pink
The value of RANDOM is 5365
***** The option is pink
The value of RANDOM is 26738
***** The option is pink
I tried changing the sleep values till 7 but still only first element gets printed..
Please help:confused:
#!/bin/bash
c=1
RANDOM=$$$(date +%s)
expressions=(pink red white yellow purple)
while [[ $c -le 9 ]]; do
echo "The value of RANDOM is $RANDOM"
selectedexpression=${expressions[$RANDOM % ${#RANDOM
[*]}]};
echo "***** The option is $selectedexpression"
sleep 1
((c++))
done