How to use the command "script" (with examples)
The “script” command is a Unix command that allows users to make a typescript file of a terminal session. It records all the output from the session, including both the input commands and their resulting output, and saves it to a file. This can be useful for sharing or documenting a terminal session or for troubleshooting purposes.
Use case 1: Start recording in file named “typescript”
Code:
script
Motivation: The motivation for using this example is to start recording a terminal session in a file named “typescript”. This is a simple and straightforward way of using the “script” command.
Explanation: When the “script” command is executed without any arguments, it starts recording the terminal session in a file named “typescript”. All the output from the session, including both the input commands and their resulting output, will be saved to this file.
Example Output:
Script started, file is typescript
Use case 2: Stop recording
Code:
exit
Motivation: The motivation for using this example is to stop the recording of a terminal session that was started using the “script” command. This allows the user to end the session and finalize the recorded file.
Explanation: The “exit” command is used to terminate the current shell session. When it is executed while the “script” command is active, it stops the recording and closes the typescript file.
Example Output:
Script done, file is typescript
Use case 3: Start recording in a given file
Code:
script logfile.log
Motivation: The motivation for using this example is to start recording a terminal session in a file with a custom name, such as “logfile.log”. This allows the user to choose the name and location of the recorded file.
Explanation: When the “script” command is executed with an argument specifying a custom file name, it starts recording the terminal session in that file. All the output from the session will be saved to this file.
Example Output:
Script started, file is logfile.log
Use case 4: Append to an existing file
Code:
script -a logfile.log
Motivation: The motivation for using this example is to append the output of a new terminal session to an existing file. This can be useful when users want to combine multiple sessions into a single file.
Explanation: The “-a” option is used with the “script” command to append the output of a new session to an existing file. By specifying the file name as an argument, the command will append the output to the end of the file instead of overwriting it.
Example Output:
Script started, file is logfile.log
Use case 5: Execute quietly without start and done messages
Code:
script -q logfile.log
Motivation: The motivation for using this example is to execute the “script” command quietly, without displaying the “Script started” and “Script done” messages. This can be useful when users want to capture the output in a file without any additional information.
Explanation: The “-q” option is used with the “script” command to execute it quietly without displaying the start and done messages. By specifying the file name as an argument, the command will record the session in the specified file without any additional messages.
Example Output: No output will be displayed when executing the “script” command with the “-q” option.
Conclusion:
The “script” command is a powerful tool for documenting and troubleshooting terminal sessions. It allows users to easily record all the input commands and their resulting output in a file. By understanding and utilizing the different options and arguments provided by the command, users can tailor the recording process to their specific needs.