Pick random line from text file with bash and shuf.

So lets say you have a text file called list.txt with entries like:

apple
orange
peach
pear
strawberry
grape
lemon
lime

What if you wanted to pick a random entry? The easiest solution is to use the ‘shuf’ tool, which is standard on CentOS as part of coreutils.

shuf -n 1 list.txt

The output would be a random line from our file!