Section 7.5. History


[Page 259 (continued)]

7.5. History

The Korn shell keeps a record of commands entered from the keyboard so that they may be edited and re-executed at a later stage. This facility is known as a history mechanism. The built-in command fc (fix command) gives you access to history. There are two forms of fc. The first, simpler form allows you to re-execute a specified set of previous commands, and the second, more complex form allows you edit them before re-execution.

7.5.1. Numbered Commands

When you're using history, it's very handy to arrange for your prompt to contain the "number" of the command that you're about to enter. To do this, set the primary prompt variable (PS1) to contain a ! character:

$ PS1='! $ '       ...set PS1 to contain a !. 103 $ _            ...prompt for command #103. 


7.5.2. Storage of Commands

The Korn shell records the last $HISTSIZE commands in the file $HISTFILE. If the environment variable HISTSIZE is not set, a default value of 128 is used. If HISTFILE is not set, or the named file is not writable, then the file $HOME/.sh_history is used by default. All the Korn shells that specify the same history file will share it. Therefore, as long as you don't change the value of $HISTFILE during a login session, the commands entered during that session are available as history at the next session. The file is not strictly a text file, but each command string is in the file (along with other housekeeping data). In the following example, I examined the history file where commands are stored using the strings command:

$ echo $HISTSIZE            ...set in ".profile". 100 $ echo $HISTFILE            ...not set previously. 
[Page 260]
$ strings $HOME/.sh_history | tail -3 ...display last 3 lines. echo $HISTSIZE echo $HISTFILE tail -3 $HOME/.sh_history $ _


7.5.3. Command Re-execution

The fc command allows you to re-execute previous commands. The first, simpler form of fc works as described in Figure 7-13.

Figure 7-13. Description of the fc shell command used for re-executing a command.

Shell Command: fc -e - [old=new] prefix

This form of the fc command re-executes the last command beginning with prefix after optionally replacing the first occurrence of the string old by the string new. prefix may be a number, in which case the numbered event is re-executed.


Here's an example of fc in action:

360 $ fc -e - ech  ...last command starting with "ech". echo $HISTFILE 361 $ fc -e - FILE=SIZE ech ...replace "FILE" by "SIZE". echo $HISTSIZE 100 362 $ fc -e - 360           ...execute command # 360. echo $HISTFILE 363 $ _ 


The token "r" is a predefined alias for "fc -e -", which allows for a more convenient way to re-execute commands:

364 $ alias r        ...look at "r"'s alias. r=fc -e - 365 $ r 364          ...execute command # 364. alias r r=fc -e - 366 $ _ 


7.5.4. Editing Commands

The Korn shell allows you to pre-edit commands before they are re-executed by using a more advanced form of the fc command, described in Figure 7-14.


[Page 261]

Figure 7-14. Description of the fc shell command used for command editing.

Shell Command: fc [ -e editor ] [ -nlr ] [ start ] [ end ]

This form of fc invokes the editor called editor upon the specified range of commands. When the editor is exited, the edited range of commands is then executed. If editor is not specified, then the editor whose pathname is stored in the environment variable FCEDIT is used. The value $FCEDIT is "/bin/ed" by default, and I don't recommend that you use this default. I personally prefer "/bin/vim" (the full pathname of the vim editor on a Linux system), as I'm most familiar with the UNIX vi editor. If no other options are specified, the editor is invoked upon the last command.

When you enter the editor, you may edit the command(s) as you wish and then save the text. When you exit the editor, the Korn shell automatically echoes and executes the saved version of the command(s).

To specify a particular command either by its index or by its prefix, supply the number or the prefix as the value of start but don't supply a value for end. To specify a range of commands, set the value of start to select the first command in the series, and set the value of end to select the last command in the series. If a negative number is supplied, it's interpreted as an offset to the current command.

The -l option causes the selected commands to be displayed but not executed. In this case, if no command series is specified, the last sixteen commands are listed. The -r option reverses the order of the selected commands, and the -n option inhibits the generation of command numbers when they are listed.


The following example illustrates the method of editing and re-execution:

371 $ whence vim            ...find the location of "vim". /bin/vim 372 $ FCEDIT=/bin/vim       ...set FCEDIT to full path. 373 $ fc 371                ...edit command # 371. ...enter vim, edit the command to say "whence ls", save, quit vi whence ls             ...display edited commands. /bin/ls               ...output from edited command. 374 $ fc 371 373      ...edit commands # 371..373. ...enter vim and edit a list of the last three commands. ...assume that I deleted the first line, changed the remaining ...lines to read "echo -n hi" and "echo there", and then quit. echo -n "hi "         ...display edited commands. echo there hi there              ...output from edited commands. 375 $ _ 



[Page 262]

Here's an example of the -l option:

376 $ fc -l 371 373     ...list commands with numbers. 371 $ whence vim 372 $ FCEDIT=/bin/vim 373 $ fc 371 377 $ fc -6             ...edit command # 371. ...edit command to say "whence ls" and then quit. whence ls               ...display edited command. /bin/ls                 ...output by command. 378 $ _ 





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net