On Unix-like operating systems, the script command makes a typescript of the terminal session.

This page covers the Linux version of script.

Description

script makes a typescript of everything printed on your terminal. It is useful for users who need a hardcopy record of an interactive session as proof of work done, as the typescript file can be printed out later with lpr. If the file argument is given, script saves all dialogue in file. If no file name is given, the typescript is saved in a file named typescript.

  • Description
  • Syntax
  • Examples
  • Linux commands help

Syntax

script [-a] [-c command] [-e] [-f] [-q] [-t[=file]] [-V] [-h] [file]

Options

The script ends when the forked shell exits (a control-D to exit the Bourne shell (sh), and exit, logout or control-d (if ignoreeof is not set) for the C-shell, csh).

Certain interactive commands, such as vi, create garbage in the typescript file. script works best with commands that do not manipulate the screen; the results are meant to emulate a hardcopy terminal.

Examples

script myfile.txt

Logs all results to file myfile.txt. This command opens a subshell and records all information through this session. The script ends when the forked shell exits (e.g., when the user types exit) or when Ctrl-D is typed.

The output will be written to the file ./myfile.txt, and the last line of the file will log the date and time the command was executed.

script -c “ps ax” /tmp/processes.txt

Logs the output of the ps command, using the a and x options, which will force it to list all current processes on the system you can see. The output will be written to the file /tmp/processes.txt, and the last line of the file will log the date and time the command was executed.

script -c “echo "Hello, World!"” hello.txt

Logs the output of the echo command that echoes the text “Hello, World!”. Since the entire command must appear in quotes, and the command contains quotes itself, the quotes in the command are escaped with a backslash. This “protects” them from the shell, telling it to treat them as literal quotes. Alternatively, we could have used single-quotes to enclose the command, and double-quotes inside the command to differentiate them.

The output will be written to the file ./hello.txt, and the last line of the file will log the date and time the command was executed.

script -c ’echo “Hello, World!”’ hello.txt

Same as the example above.