Sep 29, 2023 4 min read

How to use script Command on Linux

Use script Command on Linux with our step-by-step tutorial. All the commands that are executed in the shells are recorded by the script command.

use script Command on Linux
Table of Contents

Introduction

Before we begin talking about how to use script command on Linux, let's briefly understand – What is script command?

The script command is a built-in tool in Unix-like operating systems that records user interactions and terminal output. It captures everything from typed commands to system responses, creating a transcript of the session.

This useful command is often utilized for troubleshooting, documenting steps, and sharing instructions. With script, you can easily save and review your terminal session, making it a valuable ally for developers, system administrators, and technical support teams.

In this tutorial, you will use script command on Linux. We will also address a few FAQs on how to use script command on Linux.

How Does the script Command Work on Linux?

The syntax for the script command on Linux is as follows:

script [option] [filename]

The command's parameters and filename are optional. Simply using the script command will allow us to begin tracking our progress. If the filename is supplied, the output will be saved in that file. The options that can be used with the script command are:

aThis is used to add the log file.
cThis is used to execute the command rather than the interactive shell.
fThis will flush the output after each write.
oIf the size exceeds the specified size, this will stop the recording.
qThis will end the recording.
VThis will show the script command's installed version.
hThis will show the script command's help menu.

To find the script's installed version, use the following command:

script -V

The computer has the script installed in version 2.37.2, and we will use several examples to demonstrate how to utilize the script command on Linux.

Examples of Using the script Command

The shell's activity, including input and output, can be recorded by using the script command. Use the subsequent command to begin the recording:

script

When the command is executed, the recording of the activity begins. Use the command listed below to end the recording, and the output will automatically be saved in the "typescript" file:

exit

List the items in the directory where the commands are being run to verify the output file:

ls

The output file has been saved, and the following sections explain how to use the script command with its various options.

Example 1: Store Output in Specified File

Simply supply the file name with the script command to store the script command's output there rather than in typescript.

script vegastack.txt

Once the recording has finished, use the following command to access the "vegastack.txt" file:

cat vegastack.txt

The two commands that are denoted by red arrows in the output are carried out and recorded in the file.

Example 2: Extract the Specified Command From the Output File

We can use the "c" option with the command: to record the execution of a particular command rather than all the commands.

script -c "sudo apt update" update.txt

Display the "update.txt" output now:

cat update.txt

The update command's output has been saved.

Example 3: Append the File

We can also modify an existing script file without altering its original content. For example, we'll append the "a" option to the update.txt file:

script -a update.txt

Finish the recording after executing another command. Using the cat command, display the update.txt file:

cat update.txt

We can observe that the output is saved in the update.txt file without altering its previous content.

Example 4: Record the Screen

By using the "-timing=" option of the script command, we may additionally record the screen. The "scriptreplay" command can be used to play the saved output. For instance, in the vegastack file, we store the output:

script --timing=time_log vegastack

Finish the recording, then use the following command to play the output:

scriptreplay  --timing=time_log vegastack

The output is shown as video on the display.

Example 5: Return the error 0

Use the "e" option to create the child process error:

script -e vegastack.txt

Use the following command to stop recording and show the output:

cat vegastack.txt

The output contains the command exit code.

On Linux, that is how the script command functions.

FAQs on use script Command on Linux

How do I start recording with the script command? 

Simply open a terminal and type script followed by the desired filename to start recording. For example: script session.txt.

How do I stop recording with the script command?

To stop recording, type exit or press Ctrl+D. The terminal session will be saved as the filename specified when starting the script.

Where is the recording saved? 

The recording is saved in the current directory, using the filename provided when starting the script.

What is the purpose of using the script command? 

The script command is used to document terminal sessions, troubleshoot issues, and share instructions. It helps in reviewing past interactions and serves as a useful tool for developers, system administrators, and support teams.

Can I specify a different location for saving the recording? 

Yes, you can specify a different location by providing the desired directory path along with the filename when starting the script.

Is it possible to include timestamps in the recording? 

Yes, you can include timestamps by starting the script with the -t option. This will add timestamps to the transcript for easier reference.

Are there any limitations when using the script command? 

The script command may not capture certain graphical or non-terminal-based activities or applications. It is primarily designed to record text-based interactions within the terminal.

Conclusion

On Linux, we can use the "script" command to use the script command, which is used to log shell activities. In this tutorial, we have described how to use the script command on Linux using a different example.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.