The Linux script command has been around for ages and provides a simple but useful service. It lets you record command line activity – both input and output. This can be very helpful in troubleshooting problems or verifying what was done later by reviewing the commands that were run along with their output.
Even if you've used the script command time to time, it offers more options than many of us realize. In this post, we will look at the simplest use of script and some of the options that can make it even more useful.
The easiest way to use the script command is simply to type "script" in the terminal window and press ^d when you want to stop the recording. The output, by default, will be saved in a file called "typescript". You will see the file name that is used in the first line of output.
$ script Script started, output log file is 'typescript'. <== file name $ who shs pts/0 2021-11-16 12:10 (192.168.0.8) $ <== after ^d exit Script done.
Specify a file name
You can provide a more meaningful name if you use a command like the one shown below that adds the filename to the command:
$ script updates Script started, output log file is 'updates'. <== file name $ echo running commands running commands $ <== after ^d Exit Script done on 2021-11-16 13:30:19-05:00 [COMMAND_EXIT_CODE="0"]
The content of the updates file would look like this:
$ cat updates Script started on 2021-11-16 13:35:08-05:00 [TERM="xterm" TTY="/dev/pts/4" COLUMNS="80" LINES="24"] $ echo running commands running commands $ Exit Script done on 2021-11-16 13:35:25-05:00 [COMMAND_EXIT_CODE="0"]
Notice that the file used to record session activity includes not only the commands typed and the output generated but also the time and date that file recording began and ended.
Append output to an existing file
If you want to append the recording of session activity to an existing file, you can use the -a (or --append) option.
$ script -a updates
When you use the -c (or --command) option, the script command runs the command given and non-interactively.
To run a single command and record the interaction, you can specify the command as shown below that uses the -c (or --command) option. Note that quotation marks are needed if the command requires more than one string. The start and end times are not included in the output when this option is used.
$ script -c date Script started, output log file is 'typescript'. Thu Nov 18 03:50:06 PM EST 2021 Script done. $ $ script -c "find . -name loop -print" Script started, output log file is 'typescript'. ./bin/xtra/loop ./bin/loop ./loop ./private/loop Script done.
You can also pass a command line to script like this:
$ echo date | script Script started, output log file is 'typescript'. date $ date Tue Nov 16 02:02:10 PM EST 2021 $ exit Script done.
Run a shell script
In this next example, we use the -c option to run a script and save the interaction in a file named "myloop".
$ script -c bin/loop2 myloop Script started, output log file is 'myloop'. 1 2 3 4 Script done.
Separating input from output
On some Linux systems (like Fedora), the script command also makes it possible to separate input from output in your session recording using the -I (or --log-in) and -O (or --log-out) options. This might more easily allow you to turn the commands saved into a script since they will be in a file without the command output.
$ script -q -I in -O out $ echo Hello, World! Hello, World!
The -I (capital "i") specifies the file for input and the -O (capital "o") the file for output. Check the script man page to see what options are available to you.
The -q (or --quiet) option keeps the start and end times from being displayed on standard output (generally your terminal window).
The input file would look like this:
$ cat in Script started on 2021-11-18 16:01:58-05:00 [TERM="xterm" TTY="/dev/pts/0" COLUMNS="80" LINES="24"] echo Hello, World! Script done on 2021-11-18 16:02:06-05:00 [COMMAND_EXIT_CODE="0"]
The output file would look like this:
$ cat out Script started on 2021-11-18 16:01:58-05:00 [TERM="xterm" TTY="/dev/pts/0" COLUMNS="80" LINES="24"] $ echo Hello, World! Hello, World! $ exit Script done on 2021-11-18 16:02:06-05:00 [COMMAND_EXIT_CODE="0"]
Checking script's version
You can display the version of script that you are running like this:
$ script -V script from util-linux 2.36.2
You can read the man page for the script command or ask for explanations of the script command syntax and options with the command shown below.
$ script -h Usage: script [options] [file] Make a typescript of a terminal session. Options: -I, --log-in <file> log stdin to file -O, --log-out <file> log stdout to file (default) -B, --log-io <file> log stdin and stdout to file -T, --log-timing <file> log timing information to file -t[<file>], --timing[=<file>] deprecated alias to -T (default file is stderr) -m, --logging-format <name> force to 'classic' or 'advanced' format -a, --append append to the log file -c, --command <command> run command rather than interactive shell -e, --return return exit code of the child process -f, --flush run flush after each write --force use output file even when it is a link -E, --echo <when> echo input (auto, always or never) -o, --output-limit <size> terminate if output files exceed size -q, --quiet be quiet -h, --help display this help -V, --version display version For more details see script(1).
The script command is very handy when you want to remember, review or rerun a sequence of commands and output details on a Linux system. While the history command records the commands you enter, script is more thorough in allowing you to review command output as well as the commands that were run.
Copyright © 2021 IDG Communications, Inc.