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 (192.168.0.5)
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 (192.168.0.5)
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.


关于作者

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. Ken also has 20+ years of experience as an enterprise sysadmin with Unix, Linux, Windows, and Virtualization.

Follow him on Twitter: @kenhess for a continuous feed of Sysadmin topics, film, and random rants.

In the evening after Ken replaces his red hat with his foil hat, he writes and makes films with varying degrees of success and acceptance. He is an award-winning filmmaker who constantly tries to convince everyone of his Renaissance Man status, also with varying degrees of success and acceptance.

UI_Icon-Red_Hat-Close-A-Black-RGB

按频道浏览

automation icon

自动化

有关技术、团队和环境 IT 自动化的最新信息

AI icon

人工智能

平台更新使客户可以在任何地方运行人工智能工作负载

open hybrid cloud icon

开放混合云

了解我们如何利用混合云构建更灵活的未来

security icon

安全防护

有关我们如何跨环境和技术减少风险的最新信息

edge icon

边缘计算

简化边缘运维的平台更新

Infrastructure icon

基础架构

全球领先企业 Linux 平台的最新动态

application development icon

应用领域

我们针对最严峻的应用挑战的解决方案

Virtualization icon

虚拟化

适用于您的本地或跨云工作负载的企业虚拟化的未来