Fun unix basics with fortune and cowsay
Tags: unix, pipe, cowsay, fortune
Published: 2019-09-27 02:36:01

---
title: Fun unix basics with fortune and cowsay
published: true
description: unix tee fortune cowsay 
tags: unix, pipe, cowsay, fortune
---

```
$ cowsay "lets have fun on the command line"
 ___________________________________ 
< lets have fun on the command line >
 ----------------------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

```

The following exercise is intended to provide some simple enjoyment and cover some some unix command line concepts. This tutorial assumes you:

* are able to use a terminal i.e opening the terminal application on Mac OS and have some basic unix knowledge

* have homebrew installed or are otherwise able to install the two programs fortune and cowsay if they are not on your system
  * `brew install cowsay`
  * `brew install fortune`

There are alternate ways to install these programs on a variety of systems and in different languages.

* Open a terminal

Say Hello the old way `echo hello`.

```
$ echo hello
hello
```
Now try saying it with cowsay `cowsay hello`:


```
$ cowsay hello
 _______ 
< hello >
 ------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
```
:).


Get a pseudo random fortune epigram from the program `fortune`: 
```
$ fortune
"The History of every major Galactic Civilization tends to pass through
three distinct and recognizable phases, those of Survival, Inquiry and
Sophistication, otherwise known as the How, Why and Where phases.
"For instance, the first phase is characterized by the question 'How can
we eat?' the second by the question 'Why do we eat?' and the third by
the question 'Where shall we have lunch?'"
-- Hitchhiker's Guide to the Galaxy

```


Get your Cow to say a fortune! (by using the pipe | operator)

```
$ fortune | cowsay
 ___________________________________ 
/  "You boys lookin' for trouble?"  \
|                                   |
| "Sure. Whaddya got?"              |
|                                   |
\ -- Marlon Brando, "The Wild Ones" /
 ----------------------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
```


We are really moo'ving right along!

In this simple progression we have seen how two simple programs can interact with each other, the standard output of fortune can be redirected into cowsay which creates the ASCII cow with the enclosed text!

Lets take this piping arrangement to the next level with the program [tee](https://en.wikipedia.org/wiki/Tee_(command)) that takes stdin and then by default overwrites a file or can append to a file, while also passing that stdin to stdout to the console.

```
$ fortune | cowsay | tee save-cow-to-file.txt 

 ________________________________________ 
/ When you dial a wrong number you never \
\ get a busy signal.                     /
 ---------------------------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


$ cat save-cow-to-file.txt 
 ________________________________________ 
/ When you dial a wrong number you never \
\ get a busy signal.                     /
 ---------------------------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
```

Now we have a copy of the cow's fortune saved to a file. Lets append to that file with the -a flag on tee:

```
$ fortune | cowsay | tee -a  save-cow-to-file.txt 
 ______________________________________ 
/ Competence, like truth, beauty, and  \
| contact lenses, is in the eye of the |
| beholder.                            |
|                                      |
\ -- Dr. Laurence J. Peter             /
 -------------------------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


$ cat save-cow-to-file.txt 
 ________________________________________ 
/ When you dial a wrong number you never \
\ get a busy signal.                     /
 ---------------------------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
 ______________________________________ 
/ Competence, like truth, beauty, and  \
| contact lenses, is in the eye of the |
| beholder.                            |
|                                      |
\ -- Dr. Laurence J. Peter             /
 -------------------------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

```


You now have a pipeline going able to create a collection of generated cows with interesting epigrams written to a file! 

There is plenty of room for creative expression- check out all the flags on cowsay by checking its man page `man cowsay`.