Linux script command

Updated: 05/04/2019 by Computer Hope
script command

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.

Syntax

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

Options

-a, --append Append the output to file or typescript, retaining the prior contents.
-c,
--command command
Run the command rather than an interactive shell. This option makes it easy for script to capture the output of a program that behaves differently when its standard output is not a tty.
-e, --return Return the exit code of the child process. Uses the same format as bash termination on signal termination: exit code is 128+n.
-f, --flush Flush output after each write. This option is excellent for telecooperation: one person does "mkfifo foo; script -f foo", and another can supervise real-time what is being done using "cat foo".
--force Allow the default output destination, i.e., the typescript file, to be a hard or symbolic link. The command follows a symbolic link.
-q, --quiet Operate without displaying any output.
-t, --timing[=file] Output timing data to standard error, or to file when given. This data contains two fields, separated by a space. The first field indicates how much time elapsed since the previous output. The second field indicates how many characters were output this time. This information can replay typescripts with realistic typing and output delays.
-V, --version Display version information, and exit.
-h, --help Display a help message, and exit.

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.