The Problem
In my bash script, I would like to print both the the standard output (typically the screen) and a file.
The Solution
Below is a simple script which demonstrate the solution.
#!/bin/sh
# whatis: Demo script that prints to both screen and a file
{
echo Logging demo
echo Output will go both the screen and logging.log
# Other lines which might produce output here
} | tee logging.log
Discussion
Normally, the output from a bash script goes to the standard output, typically the screen. To redirect the output to both the standard output and a file, we employ the tee program. By surrounding the block of code with curly braces, we redirect the whole block, not just individual lines.
Awesome, exactly what I needed. Thanks
It works. Your the first one to get what I was looking for exactly right. Thank you!
Perfect, you nailed it.