"teethis" script is one of the most useful tools I've created

tee is a GNU tool to piggy back stout off of a pipe. The following example shows the basic pattern for using it, where the stout is printed to the terminal and written to a log file.

1
mycommand | tee mycommand.log

“teethis” script #

I frequently want to run shell scripts where I can see both the stdout and stderr immediately while preserving the output to refer to later. While the tee command to do this is relatively simple, it can be a pain to type over and over again, so I created the following teethis script.

1
2
3
#!/bin/bash

bash $1 2>&1 | tee $1.log

This simple script takes a bash script as the argument, and runs it while using tee to automatically save the stdout/stderr to a log file using the name of the script so that the log is easy to find.

1
2
3
4
5
6
7
# chmod +x teethis

teethis mybashscript.sh
# ... mybashscript.sh outputs ...

ls *log
# mybashscript.sh.log

Modifications that might also be frequently useful #

Append to output file, rather than overwriting

1
2
3
#!/bin/bash

bash $1 2>&1 | tee -a $1.log

Run scripts with other interpreters, called like teethis python myscript.py

1
2
3
#!/bin/bash

$1 $2 2>&1 | tee $2.log

Execute shell commands, and include date/time

1
2
3
4
#!/bin/bash

date >> teethis.log
"$1" 2>&1 | tee -a teethis.log