LinuxCommandLibrary

script

Record terminal sessions to a file

TLDR

Record a new session to a file named typescript in the current directory

$ script
copy

Stop recording
$ exit
copy

Record a new session to a custom filepath
$ script [path/to/session.out]
copy

Append to an existing file
$ script [[-a|--append]] [logfile.log]
copy

Record timing information (data is outputted to stderr)
$ script [[-t|--timing]] 2> [path/to/timing_file]
copy

Write out data as soon as it happens
$ script [[-f|--flush]] [path/to/file]
copy

Execute quietly without start and done messages
$ script [[-q|--quiet]] [logfile.log]
copy

SYNOPSIS

script [options] [file]

PARAMETERS

-a, --append
    Append the output to file or `typescript`, instead of overwriting.

-c, --command command
    Run command and exit the script when the command exits.

-e, --return
    Return the exit code of the child process.

-f, --flush
    Flush output after each write, making the script useful for monitoring in near real-time.

-q, --quiet
    Be quiet, suppress start and done messages.

-t, --timing[=file]
    Output timing data to a separate file, allowing replaying with realistic delays. If file is not specified, a default `timing` file is used.

-V, --version
    Display version information and exit.

-h, --help
    Display help message and exit.

-o, --output file
    Specify the output file. This is the default action if a non-option argument is given.

-r, --raw
    Record raw terminal output, including carriage returns. Useful for replaying.

-x, --log-timestamps
    Log timestamps of each command output, useful for analyzing activity.

-F, --force
    Use script even if stdin is not a TTY.

-v, --verbose
    Make more verbose output, showing script's internal actions.

DESCRIPTION

The script command creates a typescript of everything displayed on your terminal. It's primarily used for recording interactive sessions, often for demonstration, documentation, or auditing purposes. When script is started, it begins writing all subsequent terminal input and output to a specified file. If no file is provided, it defaults to `typescript`. The recording continues until the script process is exited, typically by typing `exit`, `Ctrl+D`, or by killing the script process.

The output file contains raw terminal data, including ANSI escape codes, which means that full-screen applications or operations like backspacing will be recorded as their control sequences rather than just the final visible state. This makes it a powerful tool for capturing the exact behavior of a command-line interface.

CAVEATS

script captures everything, including sensitive information (passwords, API keys) entered or displayed during the session. The output file can become very large quickly, especially with graphical or verbose commands. It captures raw terminal codes, which might make the output file difficult to read directly without a terminal emulator or tools like `less -R`. It is not ideal for recording applications that redraw the screen extensively (like text editors or `top`), as the recording will contain many control sequences rather than a clear view of the final state.

HOW IT WORKS

The script command works by creating a pseudo-terminal (PTY) pair. It connects your standard input and output to the "master" side of the PTY, while the "slave" side is connected to a new shell (or specified command). All data written to the slave side (by the shell) is readable from the master side, and vice versa. script then captures all data flowing through the master side and writes it to the output file.

REPLAYING RECORDINGS

To replay a session recorded with the -t (timing) option, you can use the scriptreplay command (often provided by the `util-linux` package, which also contains script). For example, `scriptreplay -t timing_file recorded_session_file` will replay the session with the original delays, mimicking the live interaction. Without timing information, you can simply `cat` the output file, or `less -R` to view it with ANSI colors and formatting interpreted.

HISTORY

The script command is a very old and fundamental Unix utility, first appearing in 4.2BSD in 1983. Its core functionality has remained largely consistent over the decades, providing a simple yet effective way to record terminal interactions. While more sophisticated session recording tools exist, script remains a widely used and often pre-installed utility for its simplicity and directness.

SEE ALSO

tee(1), cat(1), col(1), less(1), scriptreplay(1), tmux(1), screen(1)

Copied to clipboard