Using the script command to record a terminal session May 25 2020
When working on the command-line, I sometimes would like to record what I was doing to extract the exact message a script returned without having to rerun all the commands. Similarly, when I'm trying to report an error to a coworker and want to show precisely what I did. All of this can be achieved by using script(1)
. The script
command allows us to record the terminal session, including output and input to a file for later analysis. It is handy, so in this note, I'll show you how I usually use it.
Table of Contents
A basic script session
Let's begin by recording a simple session, where we are going to create a file structure and then display its content.
1
2
3
4
5
$ script
% mkdir -p main/subdirA
% mkdir -p main/subdirb
% tree main
% exit
After the execution, script
will generate a file called typescript
with all the content of our session.
If you want to review the session, you can display it using a simple cat(1)
. In my computer I get the following output:
1
2
3
4
5
6
7
8
9
10
11
12
13
$ cat typescript
Script started on Mon May 25 11:52:42 2020
bash-5.0$ mkdir -p main/subdirA
bash-5.0$ mkdir -p main/subdirB
bash-5.0$ tree main
main
├── subdirA
└── subdirB
2 directories, 0 files
bash-5.0$ exit
Script done on Mon May 25 11:52:59 2020
It recorded the time when the script session started and when it ended. You can see all the inputs I made and all the output I got during the terminal session. You can see how this be useful in many situations, for example, when you want to reproduce a bug and have the output as a reference.
The script command also let us record the timestamp for each interaction, and we can replay it as it happened during the session. Let's see how to do this.
Recording a terminal session for replay
We can ask script
to record a terminal session with typing mistakes and all. To do this, we need to pass the flag -r
.
1
$ script -r
Now you go about your business as usual and then when you end the terminal session you can play the whole session back using the -p
flag.
1
$ script -p
Using the -p
flag will show you a replay of everything that happened on your terminal session.
You can also display the whole session without the pauses (which will print everything to the screen) adding the -d
flag.
1
$ script -pd
Saving to a file
By default, the script
command saves the output to a file named typescript
. We can provide a new name by passing it as an argument to the script
command. For example:
1
$ script debug-session-2020-5-25.typescript
I chose a rather long name, but it is descriptive. You can use any name you prefer, and omit the .typescript
extension. I usually err on the side of being verbose, this to be helpful to anyone looking at my files in the future (probably me).
Recording only one command
Recording a full terminal session might not be what we want all the time. Sometimes we want to record the session for one specific command. We can do that by passing the command as an argument. The only caveat is that you will also need to pass a name for the output file. That way the script
command knows that what you are passing is a command and not the name of the output file. For example:
1
$ script -r ruby-session-2020-05-25.typescript irb
This command will record our irb
session. When the command completes, we can check our file and review the recording.
Final thoughts
The script
command is handy in many situations, and now you know how to use it. You could use it for presentations, instead of doing live coding. Or you could use it for logging purposes. It is now up to you to find smart ways to use it.
I hope it was useful.