Cowsay - How to Get Your Cow to Talk!

Note: In this article we will delve into the world of unix/linux. I use the word directory in this article. In the Microsoft Windows world we call them folders. Essentially, for our purposes, directories and folders are the same thing. Unix/linux types like to call them directories so we will stay with that convention.

Cowsay is a perl script created by Tony Monroe that generates ASCII pictures of a cow with a message.

I discovered cowsay way back in the early days of linux. It's a rather amusing little program.

cowsay says Hello world!
The cows says, "Hello world!
"


A library of cow files offer a variety of cows and other animals to select from.

As in the above image a cow can say a message or it can think a message (bubbles). A cow can also have different sates of mind:

  • Borg mode
  • Dead
  • Greedy mode
  • Paranoia
  • Stoned
  • Tired
  • Wired
  • Youthful

 I thought it would be fun to get the cow to say something more then just text typed on a terminal command line.

 

Fortune is a program that displays a pseudorandom message from a database of quotations that first appeared in Version 7 Unix.

Fortune displayed on the terminal command line.
Fortune displayed on a terminal command line.

 

Hey, why not get the cow to say a fortune?... What frivolous nerd-filled fun!

With that I wrote a simple bash script to pipe a fortune to cowsay.
Along the way I discovered that I was not the only person having fun with this.
There are many ready to use bash scripts available on the Internet.


I have since re-wrote my original cowsay bash script. It now parses what cows are available on a system, checks against a list of exclusions and ensures that the caption will fit within the terminal window. Some cows are just too silly to display or too large for the terminal window.

Fortune piped to cowsay.
Fortune piped to cowsay.

 

The script should now run on a variety of systems without a need for modification.
You can download the cowsay perl script, cowsay bash script and the cow library files here: cowsay.zip

I don't claim to be a programmer. I have not dedicate the time or training required to be proficient. So I hack away at it!
I'm sure there are better ways, better programming techniques to improve this script, however, it does work.
Constructive feedback is always welcome.

 

Installation and prepping your cow


Linux users:

For those running a modern linux distribution you can install these programs through your distro's package manager:

  • perl
  • fortune-mod
  • cows
  • zip
  • unzip

Debian, Ubuntu or Mint linux users can do this from the terminal.

Execute the following command:
sudo apt -y install perl fortune-mod cowsay zip unzip


Apple Mac users:
You will need to install Homebrew or MacPorts. After installing Homebrew or MacPorts, install these programs:

  • perl
  • fortune-mod
  • cowsay
  • zip
  • unzip.

Your current version of the bash shell may be located in directory: /opt/local/bin/
To find out where your bash shell lives, execute this command: type -a bash

If bash lives in /opt/local/bin/, edit the cowsay bash script and change the first line from:
#!/bin/bash
to
#!/opt/local/bin/bash

   No need to make this change if command: type -a bash returns /bin/bash
   If command type -a bash returns both  /bin/bash and /opt/local/bin/ use #!/opt/local/bin/


Cygwin for Windows

For Windows users install Cygwin, along with the following Cygwin packages:

  • perl
  • perl-Text-CharWidth
  • fortune-mod.
  • zip
  • unzip

 There is no cowsay package for cygwin so you will need to hunt one down or download my cowsay.zip archive from this article.

 

Cowsay archive
The cowsay.zip archive file from my website includes:

  • cowsay/bin/cowsay     Tony Monroe's perl script cowsay v3.03.
  • cowsay/cows                a directory of cow files.
  • cowsay/cowsay            my bash script.

Download and extract cowsay.zip.

Extract the archive file with this command: unzip -d ./ cowsay.zip

If you already have a working cowsay and fortune you can skip to step 4.
If you want extra cow files include step 3.

Installation
1. Copy the perl script cowsay file in cowsay/bin/ to your bin directory path:

sudo cp cowsay/bin/cowsay /<path to bin>/         example: sudo cp cowsay/bin/cowsay /usr/bin/

    Make the perl file executable: sudo +x /usr/bin/cowsay

2. Create cowthink symbolic link from cowsay in the same directory from step 1:
sudo ln -s /<path>/cowsay /<path>/cowthink        example: sudo ln -s /usr/bin/cowsay /usr/bin/cowthink

3. Copy cow files directory (cowsay/cows) to your local cows directory:
sudo cp -r cowsay/cows /<path to the cows directory>/      example: sudo cp -r cowsay/cows /usr/share/cowsay/

Discover the local cow files directory path with this command:
cowsay -l

Cow files in /usr/share/cowsay/cows:
apt bearface beavis.zen bill-the-cat bishop blowfish bong bud-frogs bunny
calvin cheese cock cower daemon default dolphin dragon dragon-and-cow duck
elephant elephant-in-snake elephant2 eyes flaming-sheep fox ghostbusters
gnu head-in hedgehog hellokitty kangaroo kilroy king kiss kitten kitty
knight koala kosh llama luke-koala mech-and-cow meow milk moofasa moose
mutilated pawn pony pony-smaller queen ren rook satanic sheep skeleton
small snort snowman sodomized stegosaurus stimpy supermilker surgery suse
telebears three-eyes tortoise turkey turtle tux udder unipony
unipony-smaller vader vader-koala www

 

For cygwin users there is no sudo command. You will need to open your cygwin terminal as Administrator:
Right mouse click on the "Cygwin64 Terminal" desktop icon and select "Run as administrator".

Launch Cygwin as Administrator.
Launch Cygwin as Administrator.

 

In the cygwin terminal execute the above commands without the proceeding sudo command.

4. Copy the cowsay bash script file to a convenient location (say in your documents or scripts directory).

    Make the bash file executable: sudo chmod +x /<path to cowsay>/cowsay
   
Test to see if it works: <path to cowsay>/cowsay

 

Edit your .bashrc file and add an alias and run cowsay command:
   alias cowsay='~/<path to cowsay>/cowsay'
   ...

   cowsay

 

Now when you launch your terminal you will be greeted by a fortune telling cow (or some other animal).

A fortune telling cow!
A fortune thinking Chameleon!

 

How the Cowsay Program Works

 

   Cowsay Program flowchart

Cowsay program flow chart.

Cowsay program flow chart.