How to Use the Script Command in Linux

How to Use the Script Command in Linux

Terminal output can be useful to review alongside the commands that produced it. The Linux script command records an interactive terminal session in a file called a typescript, which contains entered commands and visible results.

I’ll show you how to record and replay a session, and how to check its log for private input.

TL;DR: Record a Linux terminal session

Run script session.log to record terminal output in a named file. Type exit to finish the recording, or add -T when playback timing matters.

  • Without a filename, script writes to typescript.
  • Use -a to append instead of replacing an existing file.
  • Replay a timed recording with scriptreplay session.time session.log.

What is the script command in Linux?

The util-linux script command records a terminal session in a typescript file. It starts an interactive shell attached to a pseudoterminal, so the log preserves output as it appeared on screen rather than only one program’s standard output.

A typescript is a session log containing commands entered at the prompt and their visible results. Prompts and screen updates can also appear, so Bash history remains the better record when you need command lines alone. Choose script when the terminal view itself matters.

With no filename, util-linux writes to typescript in the current directory. Add a timing file when playback pacing matters.

How to record and replay a terminal session

Choose a destination file, then start the recording in the terminal you want to document. A named file makes the session clear to locate.

Step 1: Start a recording with a filename

Pass a filename to keep the recording out of the default typescript file. The command starts a subshell. Commands entered there and their terminal output are logged.

script session.log

Choose a directory where your account can create files. A distinct filename also makes it clear which session the log belongs to.

Step 2: Finish the session and inspect the log

Type exit or press Ctrl-D at the recorded shell prompt. The recording ends when that shell exits. The original shell remains available.

exit
cat session.log

Use cat or a pager to inspect a short log. For a long session, a pager lets you move through it without flooding the terminal. The file is raw terminal data, so backspaces, line feeds, and escape sequences can make some applications look messy.

Step 3: Append another session

The default behavior opens the selected log for a new recording, so use -a when you intend to preserve earlier contents and add another session to the end.

script -a session.log

Exit the recording shell as before. Appending keeps the earlier bytes, but each session’s start and finish messages remain part of the file. Give each separate run its own filename instead when you need independent logs.

Step 4: Record one command without an interactive shell

Use -c to run a command under script rather than opening the usual interactive subshell. Put a multiword command in quotes so script receives it as one string.

script -c 'uname -a' command.log

The process exits when the command completes. Add -e when a calling shell must receive the child command’s exit status.

Step 5: Save timing data and replay

To replay output at its recorded pace, create a separate timing file with -T. The typescript contains terminal output, while the timing file carries the delays and output-size information that scriptreplay needs.

script -T session.time session.log
# Run the commands to record, then type exit
scriptreplay session.time session.log

Run the replay command after the recorded shell has ended. Keep the pair of files together and use the matching pair: a timing log from one session cannot correctly describe an unrelated typescript. See the scriptreplay manual for additional options.

What should you check before recording?

Check what the log may contain and whether the application uses a normal text terminal. These limits affect whether the capture is safe to share and whether it will be readable.

Input logging and private data

The util-linux manual explains that input logging can record keystrokes the terminal does not display. Keep private data out of recordings and inspect a log before sharing it. The -I flag enables input capture:

script -I input.log

For ordinary demonstrations, omit both input-capture flags. The -B option records input and output together.

Full-screen programs can make noisy logs

Programs that redraw the screen emit control sequences that can clutter a typescript. The manual describes script as suited to output resembling a hardcopy terminal. Use an application export for a clean document:

script -c 'printf "plain report\\n"' report.log

Use script in an interactive terminal

Script starts an interactive shell, which may behave unexpectedly when input comes from a pipe. Check whether standard input is a terminal before starting:

test -t 0 && script session.log

For general command-line navigation after reviewing the log, continue with this Linux command-line guide. This is separate from writing a Bash .sh file, which automates commands rather than recording a live session.

What does the script command do?

A pseudoterminal is a software-backed pair of terminal endpoints. The shell uses one side while script writes the session data to a file. This preserves prompts and interactive output better than redirecting one command.

Use Bash history when you need entered commands alone, and an application export for structured results. A typescript is useful when you need to review the terminal display.

What is the syntax of script?

Use an explicit path when the current directory is not the right place for a log.

script [options] [file]

The filename controls where the session is saved. Use an explicit path when the current directory is not the right place for a log, and keep the timing file separate when recording playback data.

How do you start script without arguments?

Running script with no arguments starts a recording in the default typescript file and opens an interactive shell. The filename is relative to the directory from which you start the command.

script
pwd
exit

The pwd command runs inside the recorded shell, and exit ends that shell. Before using this form, check the current directory and whether a typescript file already exists there. An explicit filename avoids uncertainty about the destination.

What are the key script command options?

Use the option that matches the recording you need. These common util-linux options change the destination behavior, the captured streams, or the exit status.

OptionEffectWhen to use it
-aAppend to an existing typescriptKeep earlier content in the same log
-c commandRun a command instead of the interactive shellCapture one bounded command
-eReturn the child command statusCheck a command from a calling shell
-fFlush output after each writeWatch a growing log during a session
-qSuppress start and finish messagesKeep those notices out of a transcript
-T fileWrite timing data to a separate fileReplay the session with scriptreplay
-I fileLog inputOnly when capturing keystrokes is intended
-B fileLog input and outputWhen both streams are required

A session log also helps when a command prints progress over time. The flush option writes output as the session continues, which lets another terminal inspect a growing file. That behavior is useful for monitoring, while the default buffered behavior may suit a recording that will only be read after the shell exits.

Give each support transcript, demonstration, or command audit a distinct destination. A date or task label helps distinguish repeated sessions.

Appending is convenient for a continuing record, but it combines multiple sessions into one file. If a later session needs independent review or separate access controls, create a new file instead. The distinction is especially useful when a log may be shared with someone who should see only one task.

Keep the typescript and timing file under matching names so their relationship remains clear. The timing file stores playback pacing, not application state.

Before replaying, confirm that both files belong to the same recording and that the destination contains no unrelated data. The replay command reads the timing file alongside the typescript. If the files are moved independently, update the paths in the command rather than changing their contents.

Option details depend on the installed implementation. Check the util-linux manual on each supported distribution before relying on a flag in a portable script.

Before relying on an option in a portable script, check the installed manual page. Input-capture modes can include sensitive keystrokes.

For one-command capture, provide the command string and a destination filename. The example below records the current account identity using a read-only command.

script -c 'id' identity.log

The output is stored in identity.log. Add the exit-status option when another program needs to distinguish a successful command from a failed one.

Conclusion: Keep the typescript and timing log together

Use script to capture a terminal session, add -a when you mean to append, and create timing data with -T when replay matters. Review the files for sensitive input before sharing them.

For exact option semantics, consult the util-linux script manual and scriptreplay manual.

FAQ

These answers cover common recording questions.

How do you stop the script command in Linux?

Type exit in the shell opened by script, or press Ctrl-D. The recording ends when that interactive shell exits.

Where does script save the recording by default?

When you omit a filename, util-linux script writes the terminal recording to a file named typescript in the current directory.

Does the script command create a shell script?

No. It records a terminal session in a typescript file. A shell script is a separate file containing commands for a shell to execute.