In the context where I want to capture the stdout of a process in a file but still want to have this output displayed in the terminal I can choose between script
and tee
. In this context, are these tools essentially equivalent or is there a – possibly subtle – reason to prefer one over the other?
The programs script
and tee
are designed for different purposes:
script
-- make typescript of terminal session
tee
-- pipe fitting
Important differences between script
and tee
are:
script
transmits the exit status of the process it supervises, while tee
, being a filter, does not even know about it.
script
captures stdin, stdout, stderr of the process it supervises while tee
only catches the stream it filters.
None of these differences are relevant in the given context.
I found script
to be useful for making control sequences work when piping to tee
:
script -q -c 'python -c "import pdb, sys; pdb.set_trace()"' /dev/null \
| tee -a /tmp/tmp.txt
With only the following, Ctrl-A
would be displayed as ^A
etc:
python -c "import pdb, sys; pdb.set_trace()" | tee -a /tmp/tmp.txt
This is a minimal example. I am using tee
here to capture the output from a pytest test run, but sometimes there might be a debugger in there, and cursor keys etc should work then.
Via https://unix.stackexchange.com/a/61833/1920.
They have a very different purpose and the usage is quite different as well.
Script is to record what you are doing in a shell session. Handy to show a professor what you did, to show co-workers how to do something, etc...
Tee is just an application to write to both your screen and a file. Very handy when installing something or running a command that generates a lot of output and wanting to see the output realtime while still saving it to disk.
A notable difference between the two is that you can use script to create an interactive shell to log everything (e.g. script commands.log zsh
) including colors and such. Tee won't register as a tty so with that regard it's pretty different.