Show a random message at bash shell startup

May 14, '07 07:30:01AM

Contributed by: emont88

I really don't know what I'm doing here (my experience in shell scripting amounts to about two hours), but I put together this little script that will print out a random message each time you open a new bash window. Just copy this into your user's .bash_profile file, located in your user's home directory:

fn=/path/to/quotes.txt
cnt=$(wc -l $fn)
lns=${cnt:6:2}
if [ "$lns" = 0 ]; then
  rnd=1
else
  let "lns += 1"
  rnd=$(expr $RANDOM % $lns)
  let "rnd += 1"
fi
sed -n ${rnd}p $fn
Set the fn variable in the first line to the path and name of your quotes file. Each quote should be on its own line, with no extra lines between the quotes.

[robg adds: This worked for me -- note that this will not replace Terminal's welcome message (Welcome to Darwin!) with the quote -- it shows up after the welcome message. If you want to change the welcome message, you need to edit /etc/motd. Replacing the welcome message with a random quotation is left as an exercise for the reader (i.e. I don't have a clue...), but this hint might get you started.]

Comments (8)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20070508235210333