Skip to content

script Linux command

The Linux script command is used to record all activities in a terminal session. It captures both input and output, including error messages. This can be useful for troubleshooting, auditing, or training purposes. The recorded session is saved in a file that can be reviewed later. The script command can be invoked with various options to customize the recording behavior. It is a handy tool for tracking changes made during a session and can help in recreating sequences of commands.

script Syntax:

Terminal window
grep [options] [pattern] [file]

Options:

OptionDescription
-i, —ignore-caseIgnore case distinctions
-v, —invert-matchInvert the sense of matching
-n, —line-numberPrefix each line with line number
-c, —countSuppress normal output; instead print a count of matching lines
-r, —recursiveRead all files under each directory, recursively
-w, —word-regexpSelect only those lines containing matches that form whole words
-l, —files-with-matchesSuppress normal output; instead print the name of each input file from which output would normally have been printed
-h, —no-filenameSuppress the prefixing of filenames on output
-q, —quietQuiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected
-E, —extended-regexpInterpret pattern as an extended regular expression

Parameters:

ParameterDescription
patternThe pattern to search for in the file(s)
fileThe file or files to search for the pattern in

script bash Examples:

Record a Shell Session

Terminal window
script session.log

Starts recording a shell session and saves the output to a file named “session.log”.

Record a Shell Session with Time Stamps

Terminal window
script -t 2>session_timing.log session.log

Records a shell session with defined timestamps and saves the output to “session.log” while timestamps are saved to “session_timing.log”.

Exit the Script Session

Terminal window
exit

Stops recording a shell session started with the “script” command.

Append Output to an Existing File

Terminal window
script -a session.log

Appends the output of a new shell session to an existing file named “session.log”.

Output to a Custom File

Terminal window
script custom_session.log

Records a shell session and saves the output to a custom file named “custom_session.log”.

Start Script in a New Pseudo-Terminal

Terminal window
script -f session.log

Starts recording a shell session in a new pseudo-terminal and saves the output to “session.log”.

script Command Help Center:

How do I use script in Linux?

To use the script command in Linux, execute the following command:

Terminal window
script --option <value>

How can I save the output of a script command to a specific file?

To save the output of a script command to a specific file, use the following command:

Terminal window
script -a output.log

How do I include timestamps in the output of a script session?

To include timestamps in the output of a script session, you can use the following command:

Terminal window
script -t 2>output_with_timestamps.log

How can I start a script session with a custom shell?

To start a script session with a custom shell, you can specify it in the command like this:

Terminal window
script -c "bash --norc" output_custom_shell.log

How do I append the output of a script session to an existing file?

To append the output of a script session to an existing file, you can use the following command:

Terminal window
script -a existing_output.log

How can I log the entire script session, including all input and output?

To log the entire script session, including all input and output, you can use the following command:

Terminal window
script -f full_session.log

How do I specify the maximum time for a script session to run?

To specify the maximum time for a script session to run, you can use the following command:

Terminal window
script -c "timeout 10s bash" output_timeout.log

How can I disable the echoing of commands in the output of a script session?

To disable the echoing of commands in the output of a script session, you can use the following command:

Terminal window
script -q output_no_echo.log

Applications of the script command

  • Recording terminal sessions
  • Creating logs of terminal commands and outputs
  • Troubleshooting and debugging sessions
  • Reproducing and sharing command sequences
  • Monitoring and reviewing user interactions with the terminal