if you are interested in knowing more about how it works “script”

Contents

Illustration showing a Linux terminal screen

the script command enables you to record and replay the activity on your terminal. It is useful when you want to create a reference of the steps you took or are creating a tutorial for others to follow..

script generates typed text of all the commands you have run. It works by putting you in a sub-shell that records everything you run. Your session will be saved as plain text when you exit the shell. This does script more reliable than redirected input streams.

Basic use

the script The command can be run without any arguments:

script

A new capture session will begin. Your terminal session will be saved in ./typescript in your working directory. You can specify a different file by giving script an argument:

script example_session

Use your shell to run the script you want to record. When you finish, press Ctrl+D or write exit to exit the sub-shell. You will return to your main shell session.

You can now inspect the session log that has been created:

cat example_session

You will see the output of your script session interspersed with annotations in brackets. These annotations are used by script to record details about the terminal itself and the events that occur within it.

Since the log files are plain text, you can freely manipulate them in your favorite editor. Just cut the lines that you don't want to retain or that others shouldn't see.

This is useful if your session inadvertently generates potentially sensitive information or if you want to delete strange characters from the registry.. script records everything that happens in your terminal, including backspace keystrokes, new lines and control characters, some of which may not be necessary in the final typescript.

script outputs the name of the file you are writing to at the start and end of your session. You can disable this output by adding the -q flag to enable silent mode.

the SHELL The environment variable is read when determining the shell to branch for the internal procedure. When the variable is not set, script default to sh.

Add to an existing file

It is feasible to add new commands to an existing typescript. This enables you to “pause” and “resume” the recording by exiting your sub-shell and starting a new session.

Add the -a flag to add your commands to the specified file. script will overwrite file content when flag is not provided.

script -a example_session

Add time data

Basic typescript is ideal when you simply want to record your steps to read or share later. script you can also save time data along with your text log. This information can be used to replay your entire terminal session with appropriate delays between commands.

To run script with the -t check to specify a path to write times. Time files have a two-column format. Each entry contains the elapsed time since the last record in the first column. The second column records the number of characters that were typed, allowing repetitions with precise typing speed.

script -q -t example_session_timings example_session

Play sessions

the scriptreplay command reads typewriters and time files created by script. Play your output on your terminal. The time data will be used to match the length of each typed character to its length in its original script.

scriptreplay -t example_session_timings example_session

Some scripts may contain unwanted delays or run at an uncomfortably slow speed. Use the -d flag to speed up output. All recorded times will be divided by the number that passes.

# Run 4 times faster
scriptreplay -d 4 -t timings typescript

You can also use the -m flag to limit the maximum delay between individual updates. This enables you to specify the longest allowed pause in seconds before scriptreplay will go to the next command, even if the typescript specifies a longer delay.

# Maximum delay of 2 seconds
scriptreplay -m 2 -t timings typescript

Use with interactive commands

It is not recommended to use script with highly interactive commands. Terminal text editors are likely, curse-based applications and dialog messages insert polluting junk characters into your typed text. script focuses on capturing typical text-based output, not in commands that manipulate your terminal to produce a graphical interface.

script not suitable for use with non-interactive shells since its internal shell is always interactive in nature. The command it should not be expected to work with piped input.

Other options

the -c flag a script enables you to specify a command. When using this mode, script run the command and capture its output, instead of forking a new interactive shell. This can be more effective in capturing the output of some processes that behave differently when not directly connected to a TTY..

the -f flag instructs script to write directly to the session log after each event. This improves security by ensuring that output is captured immediately.

the script man page suggests this flag could also be used to monitor a user's terminal in real time, as in a teacher and student scenario. The student would run script -f, later the master would use SSH to connect and stream the typescript live.

script does not usually accept a symbolic link as a typed path. the --force flag enables this behavior, instructing script to solve hard and soft links to their destination.

Summary

script enables you to record your terminal sessions in plain text files with accurate time data. Can inspect, share and print the manually typed scripts or reproduce them on your terminal using scriptreplay.

Commands are included with the most popular Linux distributions. They work with all types of terminals, but individual scripts may not play correctly on a terminal other than the one used for recording. For best results, run scriptreplay in the same type of terminal that you used with script.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.