Script

From LQWiki
Jump to: navigation, search

This article is about the command script, for an explanation about the word script, see scripting.


The command script makes a transcript of everything printed on your terminal. It is useful for students who need a hardcopy record of an interactive session as proof of an assignment, as the transcript file can be printed out later with lpr(1).

It is however recommended that script should be used with col when running such programs as vi, vim, emacs or any curses-based program that modifies the terminal screen layout. As the output below shows, if you use vi or any similar tool that modifies the layout, the script file that is created contains a lot of arcane control characters.

The control characters are in fact escape sequences that directly control placement or appearance of text: blank space, new line etc. For example, ^M is carriage return, ^[ is an escape character and ^H is backspace.

A vi example (history numbers added for clarity):

{21} $ script
Script started, file is typescript
{1} $ vi hello
{2} $ exit
exit
Script done, file is typescript
{22} $ 

The contents of the file typescript contain (line breaks added for clarity):

Script started on Mon Aug 23 08:39:34 2004
 {1} $ vi hello^M^M
^[)0^[7^[[?47h^[[1;65r^[[m^[[4l^[[?1h^[=^[[H^[[2J^[[1B~^[[1B^H~
^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[
1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B
^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~
^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[
1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B
^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~
^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[1B^H~^[[
1B^H~^[[1B^H~^[[1B^H~^[[H^[[64Bhello: new file: line 1^[[H
^[[64B^[[J^[[Hhello world^M^[[64BCopying file for recov
ery...^M^[[J^[[1;12H^M^[[64B:wq^Mhello: new file: 1 line
s, 12 characters^M^[[?1l^[>^[[J^[[H~^[[K^[[63B^Hhello: new 
 file: 1 lines, 12 characters.^M^[[1B^[[65;1H^[[2J^[[?4
7l^[8^M^[[?1l^[> {2} $ exit^M^M
exit^M

Script done on Mon Aug 23 08:39:43 2004

Note that if you were to view this file using cat, you wouldn't see any of these junk characters. On the other hand, piping the file through col would probably look a lot more like the original. However, even then, scripts of editing sessions would not be very useful.


By default, the file script creates is called typescript. You can provide a filename to script, if you have to keep a regular log of the terminal session. script filename would create a file called filename in the current directory.


Provided by

Most (all?) Linux distributions incorporate this from the [util-linux] project.

Related Commands

These all relate to running commands in an altered context.

  • chroot - Confine the program to "jail".
  • env - Change variables.
  • nice - Change priority.
  • nohup - Protect from hangups (modem) or network outages.
  • stdbuf - Change buffering of standard I/O filestreams.
  • su - Change user
  • timeout - Limit the time.
  • valgrind - Validate program behavior
  • strace - Create a log of system calls.