Lesson 104: fortune Command
In this lesson, you'll learn how to use the fortune command to display random quotes, witty sayings, and fortune messages in your terminal, and how to combine it with other commands to create a more personalized and entertaining command-line experience.
The fortune command reads from a collection of text databases called fortune cookie files, stored in /usr/share/games/fortunes/ (or /usr/share/fortune/ depending on your distribution).
Each file contains a set of quotes or sayings separated by a % delimiter. When you run fortune, it picks one entry at random and prints it to the terminal.
While it may seem like a novelty, fortune has a long history in Unix culture. It was commonly added to shell startup files so every new terminal session greeted the user with a random quote, which many sysadmins still do today.
Installation
sudo apt install fortune # Debian/Ubuntu/Mint
Or:
sudo dnf install fortune-mod # Fedora/RHEL 9+
Or:
sudo yum install fortune # RHEL/CentOS 7/8
Or:
sudo pacman -S fortune-mod # Arch Linux
Or:
sudo apk add fortune # Alpine Linux
Or:
sudo zypper install fortune # OpenSUSE
Or:
sudo emerge -a sys-apps/fortune # Gentoo Linux
Syntax
fortune [OPTIONS] [CATEGORY] [FILE]
Options
| Option | Description |
|---|---|
-a |
Include offensive fortunes from the off subdirectory |
-c |
Show the category (source file) the fortune came from |
-e |
Give equal probability to all fortune files regardless of size |
-f |
List all fortune files and their percentage probability |
-l |
Show only long fortunes |
-s |
Show only short fortunes |
-n <chars> |
Set the threshold between short and long (default: 160 characters) |
-i |
Case-insensitive pattern matching (used with -m) |
-m <pattern> |
Display fortunes matching a regular expression pattern |
-o |
Show only fortunes from the offensive (off) files |
-w |
Wait before exiting (one second per line of the fortune) |
1. Display a Random Fortune
fortune
The most likely way for the world to be destroyed, most experts agree,
is by accident. That's where we come in; we're computer professionals.
We cause accidents.
-- Nathaniel Borenstein
Running fortune without arguments picks a random quote from all available non-offensive fortune databases. The output varies every time, which is the entire point.