Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here is the part that generates the 10 random numbers.

MAXCOUNT=10
count=1

while [ "$count" -le $MAXCOUNT ]; do
 number=$RANDOM
 let "count += 1"
done

Now how do I output this to an array and then echo that array?

share|improve this question
    
Why don't you print them immediately? – cuonglm Nov 28 '15 at 5:49
    
Because this is a school exercise and I'm still trying to figure out how to do things. Otherwise echoing them right away does make sense. – David Prentice Nov 28 '15 at 5:50
    
Smells like homework ;) – CousinCocaine Nov 28 '15 at 7:56
up vote 2 down vote accepted

Are you using bash? In that case, try something like that:

MAXCOUNT=10
count=1

while [ "$count" -le $MAXCOUNT ]; do
 number[$count]=$RANDOM
 let "count += 1"
done

echo "${number[*]}"

You can also replace the last line with:

echo "${number[@]}"

Some documentation here: http://www.tutorialspoint.com/unix/unix-using-arrays.htm

share|improve this answer
    
This is exactly what I was trying to do thanks! – David Prentice Nov 28 '15 at 5:49
    
Why not number[]=$RANDOM instead of number[$count]=$RANDOM? – CousinCocaine Nov 28 '15 at 7:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.