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:
-afor appending new commands and output to a previously-used file.-qfor removing the initial starting and ending statements when usingscript.--tfor 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.
Sobre el autor
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.
Más como éste
Behind the scenes of RHEL 10, part 3
Alliander modernises its electricity grid with Red Hat for long-term reliability in balance with rapid innovation
OS Wars_part 1 | Command Line Heroes
OS Wars_part 2: Rise of Linux | Command Line Heroes
Navegar por canal
Automatización
Las últimas novedades en la automatización de la TI para los equipos, la tecnología y los entornos
Inteligencia artificial
Descubra las actualizaciones en las plataformas que permiten a los clientes ejecutar cargas de trabajo de inteligecia artificial en cualquier lugar
Nube híbrida abierta
Vea como construimos un futuro flexible con la nube híbrida
Seguridad
Vea las últimas novedades sobre cómo reducimos los riesgos en entornos y tecnologías
Edge computing
Conozca las actualizaciones en las plataformas que simplifican las operaciones en el edge
Infraestructura
Vea las últimas novedades sobre la plataforma Linux empresarial líder en el mundo
Aplicaciones
Conozca nuestras soluciones para abordar los desafíos más complejos de las aplicaciones
Virtualización
El futuro de la virtualización empresarial para tus cargas de trabajo locales o en la nube