Script command lets you go back to Video on Terminal and replay your commands. This article will come in handy when you want to create a reference of the steps you’ve taken, or you’re creating a guide for others to follow.
Join the channel Telegram of the AnonyViet 👉 Link 👈 |
The script will output a formatted copy of all the commands you have run. It works by going to a sub-shell that records everything you do. The session will be saved as plain text when you exit the shell. This makes Script more reliable than redirected input streams.
Basic usage
Load Script command
The script command can be run without any parameters:
script
A new recording session will begin. Your terminal recording session will be saved to ./typescript. You can specify a different file by giving the script another parameter:
script example_session
Use the shell to execute the sequence of commands you want to record (return). When done, press Ctrl + D or type exit to exit the sub-shell. You will be back in your main shell session.
Now you can check the generated session log:
cat example_session
You’ll see your command interspersed with comments in square brackets. These comments are used by the script to record details about the terminal itself and the events occurring within it.
Since log files are plain text, you are free to modify them in a text editor.
This will be very useful if your session accidentally exposes sensitive information or you want to remove extraneous characters from the log. The script logs everything that happens in your terminal, including backspace keys, newlines, and control characters, some of which may not be necessary.
Add to an existing file
You can add other parameters to “pause” and “resume” command dialing by exiting the sub-shell and starting a new session.
Add the -a parameter to append commands to the specified file. The script will overwrite the contents of the file without this parameter.
script -a example_session
Add time data
Run the script with the -t parameter to specify a path to save the time. Time files are in a two-column format of data. Each entry contains the elapsed time since the last record in the first column. The second column records the number of characters that have been entered, allowing for accurate input speed playback.
script -q -t example_session_timings example_session
Terminal session playback
The scriptreplay command reads the output script’s data to your terminal. Timing data will be used to match each entered character. As if you will see the terminal enter commands automatically, which are recorded by the script.
scriptreplay -t example_session_timings example_session
Some recordings may have a lag that is not what you want or run at a very slow speed. Use the -d parameter to speed up the output.
# Chạy nhanh hơn 4 lần scriptreplay -d 4 -t timings typescript
You can also use the -m parameter to limit the maximum delay. The next command will be entered up to 2 seconds.
# Độ trễ tối đa 2 giây scriptreplay -m 2 -t timings typescript
Use with interactive commands
You should not use scripts with highly interactive commands. Script is focused on capturing text-based output, not commands that control your terminal. The script is not suitable for use with non-interactive shells.
Other choices
The -c parameter to the script allows you to specify a command. When this mode is used, the script runs the command and logs its output, instead of creating a new shell. This can be more efficient in capturing output data.
The -f parameter instructs the script to write directly to the session log after each event. This improves safety by ensuring output is recorded immediately.
The script’s manual page also states that this parameter can also be used to monitor the user’s terminal in real time.
Scripts generally do not accept a symlink as a path. The –force parameter allows to overcome the above situation.
summary
script allows you to record your Terminal commands into plain text files with exact time each command. You can check, share and replay them in your terminal using scriptreplay.
For best results, run the scriptreplay command in the same Terminal session that you used with the script.
Alternatively, you can also use These 5 tips to decorate the Terminal interface.