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

인공지능

고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트

open hybrid cloud icon

오픈 하이브리드 클라우드

하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요

security icon

보안

환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보

edge icon

엣지 컴퓨팅

엣지에서의 운영을 단순화하는 플랫폼 업데이트

Infrastructure icon

인프라

세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보

application development icon

애플리케이션

복잡한 애플리케이션에 대한 솔루션 더 보기

Virtualization icon

가상화

온프레미스와 클라우드 환경에서 워크로드를 유연하게 운영하기 위한 엔터프라이즈 가상화의 미래