tee it up

January 26th, 2006 | Tags:

When writing shell scripts I often need to both run the scripts manually during the day and also have them run via cron at night. I frequently log the output of the shell script to a file to reference later and email the log file.

Referred to as a pipe fitting program in its man page, tee will allow you to copy STDIN to STDOUT and display it to both the screen and a file. I’ve included a short script as an example:

#!/bin/sh
echo "Hello World!" | tee file.log

You’ll find the output Hello World! on both the terminal and in file.log. You can also tell tee to append to the file with the (-a) option. Without it you would continually write over the file with the latest output you wanted to log.

It took me a while to discover the tee command but I’ve been able to put it to great use when writing scripts that I’ll run both manually and from cron.

Comments are closed.