Search code examples
shellmonitoringtee

Are tee and script essentially equivalent?


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.


Solution

  • 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.