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 usingscript
.--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.
About the author
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.
Browse by channel
Automation
The latest on IT automation for tech, teams, and environments
Artificial intelligence
Updates on the platforms that free customers to run AI workloads anywhere
Open hybrid cloud
Explore how we build a more flexible future with hybrid cloud
Security
The latest on how we reduce risks across environments and technologies
Edge computing
Updates on the platforms that simplify operations at the edge
Infrastructure
The latest on the world’s leading enterprise Linux platform
Applications
Inside our solutions to the toughest application challenges
Original shows
Entertaining stories from the makers and leaders in enterprise tech
Products
- Red Hat Enterprise Linux
- Red Hat OpenShift
- Red Hat Ansible Automation Platform
- Cloud services
- See all products
Tools
- Training and certification
- My account
- Customer support
- Developer resources
- Find a partner
- Red Hat Ecosystem Catalog
- Red Hat value calculator
- Documentation
Try, buy, & sell
Communicate
About Red Hat
We’re the world’s leading provider of enterprise open source solutions—including Linux, cloud, container, and Kubernetes. We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.
Select a language
Red Hat legal and privacy links
- About Red Hat
- Jobs
- Events
- Locations
- Contact Red Hat
- Red Hat Blog
- Diversity, equity, and inclusion
- Cool Stuff Store
- Red Hat Summit