← All commands
THE COMMAND REFERENCE

shuf

Shuffle lines into a random order, or pick random lines.

Last updated

BusyBox

Implementation-level reference; examples are not certified for every installed version.

Overview

shuf reads lines and prints them in a random order. With -n it prints only some of them, so it can pick a random line from a list. With -e it shuffles the words you give on the command line instead of reading input. Because the order is random, the examples sort the output or count lines.

Background

shuf is part of GNU coreutils and is also an applet in BusyBox. macOS does not ship it. They agree on shuffling lines, on -n and on -e.

Examples

Shuffle the numbers 1 to 5; sorting the result shows that none was lost:

seq 5 | shuf | sort -n | paste -sd, -

Shuffle the words given as arguments with -e:

shuf -e a b c | sort | paste -sd, -

Pick three random lines from a file with -n:

seq 6 > n6.txt && shuf -n 3 n6.txt | grep -c .

Pick one random line; here the file has one line, so the answer is known:

printf 'only\n' | shuf -n 1

Common mistakes

shuf is not a source of security-grade randomness; use a proper tool for keys. Without -n, every line is read into memory. Run twice, it gives different results, so a test that compares output needs sort or a count. -n larger than the number of lines just prints all of them.

Portability

This page has no Apple view: macOS has no shuf. Common substitutes there are sort -R (which groups equal lines together) and jot -r. The Common examples were executed on GNU coreutils 9.11 and BusyBox 1.37.0 (see the development guide for the exact check).

About BusyBox shuf

BusyBox bundles shuf as an applet, and anything beyond the common options depends on how it was built. This page's examples ran on BusyBox 1.37.0.

BusyBox notes

BusyBox shuf supports the options used in the Common examples. Others, and GNU long options, depend on the build.

References

Original explanations, with upstream documentation for further reading. FreeBSD references are comparison material, not a claim of Apple version equivalence.