The Wayback Machine - https://web.archive.org/web/20241110164611/https://www.geeksforgeeks.org/script-command-in-linux-with-examples/
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.


Previous Article
Next Article

Similar Reads

Bash Script - Difference between Bash Script and Shell Script
In computer programming, a script is defined as a sequence of instructions that is executed by another program. A shell is a command-line interpreter of Linux which provides an interface between the user and the kernel system and executes a sequence of instructions called commands. A shell is capable of running a script. A script that is passed to
3 min read
Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
Shell Script to Demonstrate Wait Command in Linux
Wait command is one of the process management commands. There are different process commands in Linux mainly 5 commands are widely used which are ps, wait, sleep, kill, exit. ps is an acronym for process status. It displays information about the active processes. wait command will suspend execution of the calling thread until one of its children te
4 min read
How to Make Script Executable in Linux | chmod Command
In Unix operating systems, the chmod command is used to change the access mode of a file. The name is an abbreviation of change mode. Which states that every file and directory has a set of permissions that control the permissions like who can read, write or execute the file. In this the permissions have three categories: read, write, and execute s
7 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
Batch Script - How to check if a process is running via a Batch Script
Given a process or application (.exe) file, the task is to create a batch script that checks whether the process is running or not and output the same. Example: In this example, we have created a batch script by the name process.bat. We can easily check whether a process is currently running or not using the tasklist toolkit. Tasklist allows us to
2 min read
How to write a shell script that starts tmux session, and then runs a ruby script
Scripts are important for streamlining workflows and automating tasks. One such way to streamline workflow is by using Tmux [Terminal Multiplexer]. When used along with shell scripts we will be able to automatically commence a Tmux session and perform certain tasks. In this article, we shall see how to use a shell script that will automatically sta
4 min read
Bash shell script to find out the largest value from given command line arguments
Write a shell script to find out the largest value from the given number of command-line arguments. Example: Special variables in bash: $@- All arguments. $#- Number of arguments. $0- Filename. $1, $2, $3, $4 ... - Specific arguments. Approach If the number of arguments is 0, end the program. If not zero, then Initialize a variable maxEle with firs
1 min read
Batch Script - Echo Command
Batch script is a type of scripting language used in Windows operating systems to automate repetitive tasks, perform system administration functions, and execute a series of commands. The echo command is one of the most commonly used commands in batch scripting, used to display text or messages on the console or in a text file. In batch scripting,
8 min read
Shell Script to Display the Exit Status Using Grep Command
Linux provides users a great cool feature of the command-line tool along with a graphical user interface where they can perform tasks via ruining command. All of this command returns a status according to their execution. Its execution value can be used for showing errors or take some other action in a shell script. Exit status is following accordi
4 min read
Bash Script - Command Substitution
In order to understand command substitution, let us first discuss substitution in shell scripts. Substitution is a functionality by following which we can instruct the shell to substitute the actual value of an expression. Example: In the program below we have firstly created variable str and assigned it with the value "GeeksforGeeks" and then subs
4 min read
Bash Script - How to use Command Line Arguments
In this article, let us see about Command Line Arguments usage in Bash script. Arguments are inputs that are necessary to process the flow. Instead of getting input from a shell program or assigning it to the program, the arguments are passed in the execution part. Positional Parameters Command-line arguments are passed in the positional way i.e. i
4 min read
Batch Script - Printing / NETPrint Command
A Bash script is similar to a simple text file that contains a number of commands that the programmer can write in a command line. In Unix-based systems, these commands are used for performing repetitive tasks. A Bash script consists of a bunch of commands, or it may contain elements like loops, functions, conditional constructs, etc. In other word
3 min read
Shell Script which Works Similar to the Unix Command HEAD TAIL
Prerequisites: Bash Scripting, Shell Function Library HEAD and TAIL command print a specified number of lines of a file from the beginning and from the end respectively. If we don't specify a number, the commands print the entire file. We will make a bash script display.sh which takes two to three arguments. -h or -t option to specify whether to pr
5 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
3 min read
fmt command in Linux with examples
fmt command in LINUX actually works as a formatter for simplifying and optimizing text files. Formatting of text files can also be done manually, but it can be really time-consuming when it comes to large text files, this is where fmt comes to rescue. fmt re-formats each paragraph in the file specified, writing to standard output. Here's the syntax
4 min read
anacron command in linux with examples
anacron command is used to execute commands periodically with a frequency specified in days. Its main advantage over cron is that it can be used on a machine which is not running continuously. In cron if a machine is not running on time of a scheduled job then it will skip it, but anacron is a bit different as it first checks for timestamp of the j
3 min read
expect command in Linux with Examples
expect command or scripting language works with scripts that expect user inputs. It automates the task by providing inputs. // We can install expect command using following if not installed // On Ubuntu $sudo apt install expect // On Redhat based systems $ yum install expect First we write a script that will be needing inputs from users and then we
2 min read
function command in Linux with examples
The function is a command in Linux that is used to create functions or methods. It is used to perform a specific task or a set of instructions. It allows users to create shortcuts for lengthy tasks making the command-line experience more efficient and convenient. The function can be created in the user's current shell session or saved in the shell
2 min read
Clear Command in Linux with Examples
clear is a standard Unix computer operating system command that is used to clear the terminal screen. This command first looks for a terminal type in the environment and after that, it figures out the terminfo database for how to clear the screen. And this command will ignore any command-line parameters that may be present. Also, the clear command
2 min read
Continue Command in Linux with examples
continue is a command which is used to skip the current iteration in for, while, and until loop. It is used in scripting languages and shell scripts to control the flow of executions. It takes one more parameter [N], if N is mentioned then it continues from the nth enclosing loop. The syntax for the `continue` command in Linuxcontinue or continue [
2 min read
hostid command in Linux with examples
hostid is a command in Linux that is used to display the Host's ID in hexadecimal format. It provides a quick and straightforward way to retrieve the host ID, allowing administrators to associate it with software licenses or perform system-specific operations. Syntax of `hostid` command in Linuxhostid [OPTION] Options provided by hostedhostid --hel
1 min read
chpasswd command in Linux with examples
chpasswd command is used to change password although passwd command can also do same. But it changes the password of one user at a time so for multiple users chpasswd is used. Below figure shows the use of passwd command. Using passwd we are changing the password of the guest user. Here first you have to enter the password of the currently signed u
2 min read
lsmod command in Linux with Examples
lsmod command is used to display the status of modules in the Linux kernel. It results in a list of loaded modules. lsmod is a trivial program which nicely formats the contents of the /proc/modules, showing what kernel modules are currently loaded. Syntax: lsmod Example: Run lsmod at the command line to list all active kernel modules. lsmod Output:
1 min read
emacs command in Linux with examples
Introduction to Emacs Editor in Linux/Unix Systems: The Emacs is referred to a family of editors, which means it has many versions or flavors or iterations. The most commonly used version of Emacs editor is GNU Emacs and was created by Richard Stallman. The main difference between text editors like vi, vim, nano, and the Emacs is that is faster, po
5 min read
host command in Linux with examples
host command in Linux system is used for DNS (Domain Name System) lookup operations. In simple words, this command is used to find the IP address of a particular domain name or if you want to find out the domain name of a particular IP address the host command becomes handy. You can also find more specific details of a domain by specifying the corr
2 min read
yes command in Linux with Examples
yes, the command in Linux is used to print a continuous output stream of a given STRING. If STRING is not mentioned then it prints 'y'; Syntax: of `yes` command in Linuxyes [STRING] Note: To stop printing please press Ctrl + C. Use of `yes` commandlet us say that we want to delete all the `.txt` files present in the current directory. Instead of wr
2 min read
bzdiff command in linux with examples
bzdiff command in Linux is used to compare the bzip2 compressed files. Bzcmp and bzdiff are used to invoke the cmp or the diff program on bzip2 compressed files. All options specified are passed directly to cmp or diff. If only one file is specified, then the files compared are file1 and an uncompressed file1.bz2. If two files are specified, then t
1 min read
bzcmp command in Linux with examples
The bzcmp utility is used to invoke the cmp utility on bzip2 compressed files. All options specified are passed directly to cmp. As internally bzcmp calls cmp, We can pass the cmp command options to bzcmp. If only one file is specified, then the files compared are file1 and an uncompressed file1.bz2. If two files are specified, then they are uncomp
2 min read
three90RightbarBannerImg