Make Your Own Fortune

Last edited on 2026-06-20 Tagged under  #bsd   #linux   #shell   #programming 

As we all go about our day we discover bits of wisdom that WOW! and YES! seem to be directed at you.

I like to put these bits together in a file for later review. Using the programs fortune and cowsay I made a little shell script that runs at a console login or when a terminal window/tab is opened and outputs a random fortune from my custom collection.



Fortune

The fortune command is usually available to install as the fortune or fortune-mod package on most (all?) Linux distributions and the BSDs.

Running the fortune or fortunes command prints an adage chosen at random from database files stored (on Gentoo) in /usr/share/fortune:

-> fortune
I'll defend to the death your right to say that, but I never said I'd
listen to it!
		-- Tom Galloway with apologies to Voltaire

Customize

Create the my_fortune file (I place mine in ~/.config):

vi ~/.config/my_fortune

Add blocks of text separated by the % percent symbol. Example:

All creatures love life. All creatures fear death. Therefore do not kill or help others to kill.
-- The Buddha
%
Everyone takes the limits of his own vision for the limits of the world.
-- Arthur Schopenhauer
%
Non est ad astra mollis e terris via (there is no easy way from the earth to the stars).
-- Seneca
%

Save changes and exit.

Convert this file to a format that fortune can use with the included strfile command, which generates the necessary *.dat file type:

strfile -c % ~/.config/my_fortune ~/.config/my_fortune.dat

NOTE
Both my_fortune and my_fortune.dat need to be located in the same directory.

Display your own fortune:

-> fortune ~/.config/my_fortune
The price of anything is the amount of life you exchange for it.
-- Henry David Thoreau

Cowsay

What is better than seeing your own fortune in the console? Why having a cow deliver it!

Install the cowsay package with your BSD or Linux package manager, then pipe your fortune to the cow:

-> fortune ~/.config/my_fortune | cowsay
 _________________________________________ 
/ If you don’t get everything you want, \
| think of the things you don’t get     |
| that you don’t want.                  |
\ -- Oscar Wilde                        /
 --------------------------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

I actually prefer my fortunes to be delivered by a kitty:

-> fortune ~/.config/my_fortune | cowsay -f kitty
 _________________________________________
/ I have learned silence from the         \
| talkative, toleration from the          |
| intolerant, and kindness from the       |
| unkind; yet strange, I am ungrateful to |
\ these teachers. -- Kahlil Gibran        /
 -----------------------------------------
     \
      \
       ("`-'  '-/") .___..--' ' "`-._
         ` *_ *  )    `-.   (      ) .`-.__. `)
         (_Y_.) ' ._   )   `._` ;  `` -. .-'
      _.. `--'_..-_/   /--' _ .' ,4
   ( i l ),-''  ( l i),'  ( ( ! .-'

Script

I created a shell script for these commands. It displays a cow-ified fortune if the system has cowsay + fortune, falls back to a plain fortune if cowsay is also missing, and errors out if no my_fortune file or fortune command is found.

Verify that $HOME/bin directory is present and in your PATH:

echo $PATH

The script, saved as my_fortune and placed in ~/bin:

#!/bin/sh
#
# Purpose: Display a random fortune from my custom fortunes file

mesg="$HOME/.config/my_fortune"
fort="command -v fortune"
cows="command -v cowsay"

if [ -f $mesg ]; then
  if $fort 2>&1 >/dev/null && $cows 2>&1 >/dev/null; then
    fortune $mesg | cowsay -f kitty
  elif $fort 2>&1 >/dev/null; then
    fortune $mesg
  else
    echo "(O< error: script $0 requires: fortune; recommends: cowsay"
    echo "(/)_"
    exit 1
  fi
else
  echo "(O< error: $mesg not found."
  echo "(/)_"
  exit 1
fi

Make the script executable:

chmod 755 ~/bin/my_fortune

Run:

my_fortune

...to display your own jazzy fortune!

Auto-run

To have my_fortune run whenever logging into a console or opening a terminal window/tab, add the script to the shell's config file.

I use the fish shell, and I modify the config.fish file:

vi ~/.config/fish/config.fish

Add:

if status is-interactive
    if command -v my_fortune > /dev/null
        my_fortune
    end
end

Save changes and exit.

For bash, modify .bashrc:

vi ~/.bashrc

Add:

if command -v my_fortune 2>&1 >/dev/null
then
    my_fortune
fi

Save changes and exit.

Modify as appropriate (.shrc for sh, etc) for your chosen shell.

Its a fun and often enlightening nudge to start the day!

 _________________________________
< Live long and prosper. -- Spock >
 ---------------------------------
     \
      \
       ("`-'  '-/") .___..--' ' "`-._
         ` *_ *  )    `-.   (      ) .`-.__. `)
         (_Y_.) ' ._   )   `._` ;  `` -. .-'
      _.. `--'_..-_/   /--' _ .' ,4
   ( i l ),-''  ( l i),'  ( ( ! .-'

You can like, share, or comment on this post on the Fediverse 💬

Thanks for reading! Read other posts?

« Previous: Just Enough Gentoo