Command Line – How to Randomize the Output from seq

.seqcommand line

I know I can use seq to generate a random list of numbers: 1, 2, 3, 4…

I want to get those numbers into a random order like 3, 1, 4, 2…

I know I can use shuf to shuffle the lines of a file. So I could use seq to write random numbers to a file and then use shuf to shuffle them — or write some sort of shuffle function. But this seems needlessly complex. Is there a simpler way to randomize the items in an array with a single command?

Best Answer

You can just pipe the output to shuf.

$ seq 100 | shuf

Example

$ seq 10 | shuf
2
6
4
8
1
3
10
7
9
5

If you want the output to be horizontal then pipe it to paste.

$ seq 10 | shuf | paste - -s -d ' '
1 6 9 3 8 4 10 7 2 5 

$ seq 10 | shuf | paste - -s -d ' '
7 4 6 1 8 3 10 5 9 2 

$ seq 10 | shuf | paste - -s -d ' '
9 8 3 6 1 2 10 4 7 5 

Want it with commas in between? Change the delimiter to paste:

$ seq 10 | shuf | paste - -s -d ','
2,4,9,1,8,7,3,5,10,6
Related Question