Linux Basics: Sample Lines with GNU shuf
A practice queue can take a random subset of a short work list. GNU shuf samples input lines without replacement by default. Test membership and counts rather than publishing one random ordering as an expected result.
Prepare a scratch directory
Tested on Linux with GNU coreutils 9.7 and GNU Bash 5.2.37. You need the named runtime, Bash, a text editor, and the standard mktemp, rm, and rmdir utilities. The script creates a separate temporary fixture directory and removes only that directory on exit. It does not operate on your existing files.
Keep using the same terminal. Stop if setup fails, before saving the source:
start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1Run the example
Save this as demonstrate-gnu-shuf.sh:
#!/usr/bin/env bash
set -euo pipefail
export LC_ALL=C
fixture_dir="$(mktemp -d)"
trap 'rm -rf -- "$fixture_dir"' EXIT
cd -- "$fixture_dir"
printf 'alpha\nbeta\ngamma\ndelta\n' > candidates.txt
shuf -n 2 candidates.txt > selected.txt
test "$(wc -l < selected.txt)" -eq 2
test "$(sort -u selected.txt | wc -l)" -eq 2
while IFS= read -r value; do
grep -Fxq -- "$value" candidates.txt
done < selected.txt
printf 'Two unique input lines selected: yes\n'
shuf -i 1-3 -n 3 | sort -n > range.txt
printf 'Complete shuffled range, sorted for verification:\n'
cat range.txt
test "$(cat range.txt)" = "$(printf '1\n2\n3')"
shuf -r -n 4 -e only > repeated.txt
test "$(wc -l < repeated.txt)" -eq 4
test "$(sort -u repeated.txt)" = only
printf 'Replacement permits four copies of one input: yes\n'Run it:
bash demonstrate-gnu-shuf.shObserved output:
Two unique input lines selected: yes
Complete shuffled range, sorted for verification:
1
2
3
Replacement permits four copies of one input: yesRead the result
The first sample contains two different input lines because the fixture itself has unique lines and sampling is without replacement. The script verifies every selected value belongs to the input. The integer-range example samples the whole range, then sorts only for stable display. -r enables replacement, allowing the same input line to appear repeatedly.
Apply it carefully
Identical text on separate input lines can still produce repeated text without -r; uniqueness here depends on the fixture. A random sample is not a security token or a statistical validation of the random source. Always bound replacement mode with -n when a finite result is required. For exact reproducibility, design and record an explicit random-source policy rather than expecting this command to provide a convenient seed option.
The official reference describes the API and its options. The checks above cover the specific fixture, not every possible input or environment.
Clean up
Remove the source and then the empty practice directory:
rm -- demonstrate-gnu-shuf.sh
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dirIf the directory is not empty, inspect the remaining files rather than deleting unknown contents.
Acknowledgements
This post was created with assistance from AI.