Custom fortune in MOTD/greeting

Reading time: 2 minute(s).
2022-12-12

I display some custom text using fortune whenever I open a new terminal. Since a few people have asked me how do I do it, I'll write it here as a sort of reminder/reference, even if it's a very simple man fortune away.

Supplying fortune with custom file is simple: all we need to do is create a file with our epigrams separated by %s, like so:

Bash
~/fortune $ ls
test
~/fortune $ cat test
This is an epigram!
%
This is another epigram!
~/fortune $

Then, we need to run strfile on our test file, producing a test.dat file in the process. This file is used by fortune to quickly find epigrams.

Bash
~/fortune $ strfile test
"test.dat" created
There were 2 strings
Longest string: 25 bytes
Shortest string: 20 bytes
~/fortune $ ls
test test.dat

We can now run fortune test inside that folder.

Bash
~/fortune $ fortune test
This is another epigram!
~/fortune $ fortune test
This is an epigram!
~/fortune $ fortune test
This is another epigram!

You can repeat the process for any number of files. In my case, I wrote a fish script that runs strfile for every text file in the folder, without a .fish or a .dat extension:

Fish
~/fortune master ↑1 λ❱ cat mkfortunes.fish
#!/usr/bin/fish

for file in (string match -rv ".*\.fish|.*\.dat" *); 
  strfile $file;
  # echo $file;
end

And then, in my fish_greeting.fish, I have this command:

Fish
fortune (string match -rv ".*\.fish|.*\.dat" ~/fortune/*)

This won't work in bash or in your bashrc, but it simply gets every file in my fortune folder without a .fish or .dat extension and feeds that to fortune.

While we're at it, I also suggest to create a git repository to keep track of your fortune folder. You never know!

top↑ end↓