Open In App

script command in Linux with Examples

Last Updated : 03 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The ‘script’ command in Linux is a versatile tool that allows you to record all terminal activities, including inputs and outputs, making it a valuable resource for developers, system administrators, educators, and anyone who needs to document terminal sessions. This command captures everything displayed on your screen during the session, saving it in a file called a typescript. By default, if no filename is specified, the output is saved to a file named ‘typescript’. The ‘script’ command is especially useful for logging command outputs, capturing installation processes, compiling open-source code, and creating educational demonstrations.

What Does the ‘script’ Command Do?

The ‘script’ command records a terminal session, creating a complete log of all inputs and outputs. This makes it a powerful tool for:

  • Demonstrate command usage, troubleshoot issues, or share command-line processes with others.
  • Record terminal outputs during software installations, compiling processes, or while running scripts to create detailed logs.
  • Capture error messages and terminal outputs to share with support teams or for personal review.
  • Maintain a record of actions performed on a system for compliance and auditing purposes.

Syntax:

script [options] [file]

script Command Examples in Linux

Example 1: Starting a Typescript Without Arguments

To start a typescript without any argument. If no filename is given as argument, ‘script’ will automatically create a file namely ‘typescript’ in the home directory to save the recorded information. 

Input:In order to stop the typescript, we just need to execute exit command and script will stop the capturing process. Since there’s no filename given as argument, the script will automatically create a file namely typescript in the home directory to save the recorded information. 

Output: 

Example 2: Recording Terminal Output to a Specific File

To start the typescript, run any random command and save it in a text file, let’s say ‘geeksforgeeks.txt’. 

Input: 

 Output: 

The output produced above is the content of the file ‘geeksforgeeks.txt’, created by ‘script’ command. 

Key Features and Options of the ‘script’ Command

1. ‘-a, –append’:

This option is used when we want to append the output, retaining the prior content of the file. The multiple contents get separated by adding a line that states the date and time of the script started. 

Example: 
Input:
  
Output: 

2. ‘-c, –command’

This option is used when we want to run a particular command rather than interactive shell and get terminal information in the file given as argument or typescript by default. The script will automatically exit after successful execution. 

Example: To get the typescript of ‘cal’ command. 
Input:
  
Output: 

3. ‘-e, –return’

Return the exit status of the child process, which is useful for scripting scenarios where the success or failure of commands needs to be tracked.

4. ‘-f, –flush’

Flush the output after each write, making it useful for real-time monitoring. This is especially handy for collaborative work or live demonstrations.

5. ‘–force’

This option allows default output file i.e. typescript to be hard or symbolic link. 

Example: To capture terminal activity in a file let’s say ‘gfg2′ that is stored in ‘/home/sc’.
Input: 

Output: 

6. ‘-q, –quiet’

This option does not display the notification stating that the script has started and quietly execute and exit the ‘script’ command. 

7. ‘-t, –timing[=]’

This option allows user to capture the terminal activity step by step and appears like a video when the recorded file is executed with the help of ‘scriptreplay’ command. 
Example: To capture terminal activity in a manual file, ‘geeksforgeeks1′
Input: 

This option contains two data fields. The first field indicates how much time elapsed since the previous output. The second field indicates how many characters were output this time. Now let’s check the output created using another command i.e. ‘scriptreplay’ as follow:

scriptreplay --timing=time_log geeksforgeeks1

Output:

8. ‘-V, –version’

Output version information and exit. 

9. ‘-h, –help’

Display this help and exit.

script command in Linux with Examples – FAQs

What is the script command used for in Linux?

The script command in Linux is used to record everything displayed on your terminal. It captures a session of your terminal into a file, including all inputs and outputs. This tool is particularly useful for educational purposes, demonstrations, troubleshooting, and logging terminal activities. It records not just commands but also their output, making it a comprehensive tool for capturing terminal sessions.

How to start recording a terminal session with script?

To start recording a terminal session with the script command, simply type script followed by the filename where you want to save the session. For example:

script session_output.txt

This command will start a new sub-shell, and all subsequent terminal input and output will be recorded into session_output.txt. If you do not specify a filename, script typically uses the default filename typescript.

Can script capture all terminal output, including graphics?

The script command is designed to capture text output that appears in the terminal. It does not capture graphical output. If your applications output non-text content (like images or GUI-based content), those won’t be recorded by script. The command is best suited for command-line activities and text-based data.

How to stop recording a session with script?

To stop recording a session while using the script command, you simply need to exit the shell that script started when you began recording. You can do this by typing exit or pressing Ctrl-D:

exit

This command will terminate the script session and return you to your normal shell session. The file where the session was recorded will be saved and closed.

What are some common options for the script command?

The script command offers several options that can be useful depending on your needs:

  • -a, –append: Append the output to the file or typescript, preserving the existing contents.
  • -f, –flush: Flush output after each write. This is useful for real-time monitoring of the script session via another terminal.
  • -q, –quiet: Do not show start and stop messages, a useful option for scripts where you don’t want to clutter the output with unnecessary information.
  • -c, –command command: Run the script command as a command, which can be particularly useful for automating tasks or logging.
  • -e, –return: Return the exit status of the child process, which can be utilized in scripts to determine the success or failure of the commands run within the script session.


Next Article

Similar Reads

three90RightbarBannerImg
  翻译: