40

I wish to display a cowsay message in every terminal window. The command I use is

 fortune | cowsay

Any ideas how to make it appear whenever I start a new terminal session?

6 Answers 6

48
  • Open the file ~/.bashrc (or /etc/bash.bashrc if it should work globally for all users).

  • Add the following at the bottom of the file:

    if [ -x /usr/games/cowsay -a -x /usr/games/fortune ]; then
        fortune | cowsay
    fi
    
4
  • 1
    does the -x flag mean "if exists" ? Dec 6, 2010 at 16:58
  • 3
    Yes, "-x" tests if that file exists and is executable. For more information on the the test program see its man page: man test ([ ... ] is short for test ...).
    – htorque
    Dec 6, 2010 at 17:03
  • 1
    Works for bash on Windows 10 also!
    – LShaver
    Aug 14, 2016 at 21:10
  • added clear and then the above command to the end .profile file as that is what is called on logins. Dec 17, 2020 at 7:06
8

BONUS: If you're using bash you can make a ~/.bash_logout file and it will execute that when you log out. Make sure to have it sleep for a second or so, or you'll never see it.

cowsay "See you later"; sleep 1

1
  • wow, thats neat! Ill do it immediately! Jan 24, 2011 at 7:54
7

I would consider setting a limit to the size of the fortune command requested.

fortune -s | cowsay

This will produce at most 160 characters. This is a good size for a greeting, anything more than that and long fortunes will flood your terminal.

4

I use

fortune | cowsay -f `ls -1 /usr/share/cowsay/cows/ | sort -R | head -1` -n

This will display a random fortune using a random 'cow'. There's probably a better way to get a random 'cow', but this works for me just fine.

2
  • 1
    For Mac: fortune | cowsay -f `ls -1 /usr/local/Cellar/cowsay/3.04/share/cows/*.cow | sort | head -1` -n
    – jibran
    Oct 17, 2016 at 7:08
  • Whoa I knew mac was Unix-based, didn't realize it even had cowsay and stuff!! Jun 5, 2018 at 7:22
3

Better you use .bash_profile because .bashrc is also executed at commands like scp and they will fail silently if there is any output

I found a nice script on pastebin. currently using it.

http://pastebin.com/f0QXTPbt

0

I am using

cowsay Welcome to Bash Shell in .bashrc.

This will be prompt every time when you open the terminal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.