Skip to main content

How to capture terminal sessions and output with the Linux script command

The Linux script command allows you to create replayable terminal sessions by simply entering commands.
How to capture terminal sessions using the script commandPhoto by Sora Shimazaki from Pexels

Photo by Sora Shimazaki from Pexels

The Linux script command creates a typescript file from your terminal session. This means that if you invoke the script command, you are dropped to a "watched and recorded" terminal session subshell that's saved to an ASCII text file. When created with a timing file, you can replay the session, including output. The purpose of script is that you can easily grab sample output from any command through an interactive session exactly as it's displayed in your terminal. You can use backspace, edit files, create files, and run simple or complex commands.

[ Readers also liked: Linux Command Basics: 7 commands for process management ]

The value of the script command is in its capability to capture output during your terminal session for any terminal command without redirects, which don't always work. I was frustrated so many times when attempting to capture output from a command that somehow is going awry until I discovered script. With standard redirect operators, some output can be redirected to a file, while other commands will only show output in stdout or the screen. Most sysadmins use the script command to show output during software installation, when troubleshooting, or for development and programming purposes.

Surprisingly, the script command does not help you create shell scripts.

Script options

As with most commands that I use, I only use a subset of available options for them. The script command has several options that I've never found useful in my own work. The only ones I use are:

  • -a for appending new commands and output to a previously-used file.
  • -q for removing the initial starting and ending statements when using script.
  • --t for saving timing information for playback.

When I use script, I always use --t to create a timing file and -q for quiet mode. I only use -a when I need to append more info into an existing script file, which is rare.

Script usage

The following are two standard examples of the way I use script:

$ script --t=<logfile> -q <script file>

And, to append to script file:

$ script --t=<logfile> -q -a <script file>

Where logfile and script file can be names that you choose. When you want to end and save the file, use Ctrl-D on your keyboard. You can look at, edit, or remove the script file and the log file at will. They are simple ASCII text files.

Here is an example:

$ script --t=script_log -q scriptfile

I ran the ls command, the who command, and then I ended the script with Ctrl-D.

$ ls
blah.txt  test1 test2 doc.txt
$ who
root     tty1         2021-01-18 09:31
khess    pts/0        2021-01-20 14:42 (
khess    pts/1        2021-01-20 14:47
$ exit

When you press Ctrl-D, the script exits and displays exit.

Use the cat command to display the contents of scriptfile.

$ ls
blah.txt  file_time   scriptfile  script.rec    shell_record1  shell_record3  time_log
file_log  record.scr  script_log  scriptrecord  shell_record2  snap           typescript
$ who
root     tty1         2021-01-18 09:31
khess    pts/0        2021-01-20 14:42 (
khess    pts/1        2021-01-20 14:47
$ exit

Script done on 2021-01-20 14:47:28-06:00

If you want, you can also cat the script_log file.

$ cat script_log
0.088699 31
3.393729 1
0.246070 1
0.540094 2
0.003060 196
0.000195 31
2.136900 1
0.177266 1
0.179336 1
0.540818 2
0.003883 134
0.000210 31
4.676286 6

This is the timing log file that behaves similar to a transaction log for your script commands and responses. It is important when you play back the file, which I demonstrate in the follow-up article, How to replay terminal sessions recorded with the Linux script command.

[ Learn the basics of using Kubernetes in this free cheat sheet. ] 

Wrap up

For me, the best application of the script command is for training new Linux users on how to use commands and to show them expected output in real-time, as if they were interacting with the terminal session themselves. For more experienced users, you could create a training session that teaches a new software installation or configuration. Training is the application I think of because of my history with training new sysadmins and writing how-to articles for various venues. And since the output is in ASCII text files, you can change the output for your own needs and audiences.

Check out these related articles on Enable Sysadmin

Topics:   Linux   Command line utilities  
Author’s photo

Ken Hess

Ken Hess is an Enable SysAdmin Community Manager and an Enable SysAdmin contributor. Ken has used Red Hat Linux since 1996 and has written ebooks, whitepapers, actual books, thousands of exam review questions, and hundreds of articles on open source and other topics. More about me

On Demand: Red Hat Summit 2021 Virtual Experience

Relive our April event with demos, keynotes, and technical sessions from
experts, and sign up to attend breakout sessions June 15–16.

Related Content