Linux Command Reference ยท System Information
Linux script command
Reference for the script command: record everything a terminal session prints to a typescript file, run one command, log timing, and replay it with scriptreplay.
script starts a subshell and records everything that appears on the terminal into a file called a typescript (not related to the TypeScript language). It is the quickest way to capture a long installation, a bug reproduction or a training session so that you can attach it to a ticket or review it later.
When you leave the subshell with exit or Ctrl+D, recording stops. With -c COMMAND it runs one command instead of an interactive shell, which also makes it usable in scripts.
script [options] [FILE]
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
script [-aefqrc] [--timing=file] [--log-in file] [--log-out file] [file] scriptreplay [-t timingfile] [-s typescript]
| Name | Description |
|---|---|
file |
The typescript to write. The default is typescript in the current directory. |
-a, --append |
Append to the file instead of truncating it. |
-c COMMAND, --command |
Run COMMAND instead of an interactive shell; script exits when it finishes. |
-e, --return |
Return the exit status of the child process. |
-f, --flush |
Flush output after each write, so another terminal can tail -f the file. |
-q, --quiet |
Do not print the "Script started" and "Script done" messages on the terminal. (The file itself still starts with a Script started on ... line and ends with a Script done line, both with timestamps.) |
-t[file], --timing[=file] |
Write timing data (delay and byte count per chunk) for scriptreplay. |
-I FILE, --log-in |
Also log what you type (keyboard input) into FILE. |
-O FILE, --log-out |
Log output only, without the header and footer. |
-E when, --echo when |
Control echoing of input: auto, always or never. |
Returns: by default 0 even if the recorded command failed; with -e the exit status of the child.
Errors and edge cases: the file contains raw terminal data, including carriage returns, colour codes and backspaces. Strip them with col -b, sed 's/\x1b\[[0-9;]*m//g' or tr -d '\r' before reading it in an editor.
Usage notes
- Passwords are recorded. Anything the terminal echoes ends up in the file. Passwords you type are normally not echoed, but with
-Ithe typed characters are logged. Treat typescripts as sensitive and never share them unreviewed. - Interactive programs such as
vimortopproduce huge files full of escape sequences. Record sessions of plain command output. - Replay.
scriptreplay -t times.txt -s out.logplays the session back with its original delays. Add a divisor (scriptreplay -t times.txt -s out.log 2) to speed it up. - Pipes and logging.
scriptneeds a terminal pseudo-device; when the shell is non-interactive (as in cron) it still works with-c, but there is no real tty size or input. - Nested sessions. Starting
scriptinsidescriptrecords the inner session twice. The environment variableSCRIPTis not set by default, so remember to exit before starting another. - Alternatives.
teerecords output only if the program writes plainly and cannot handle interactive programs (tee).asciinemarecords a shareable, replayable web format, but is not installed by default. - Where it comes from.
scriptandscriptreplayare part of util-linux; the BSDscripthas different options (-k,-r,-p). - Cleaning the output.
scriptwrites^Mcarriage returns on Linux;dos2unixortr -d '\r'fixes that.
Related
tee,history,clear- Lessons: Streams, Logging with journald