7.5. HistoryThe 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 CommandsWhen 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 CommandsThe 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. 7.5.3. Command Re-executionThe fc command allows you to re-execute previous commands. The first, simpler form of fc works as described in Figure 7-13.
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 CommandsThe 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.
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 $ _ 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 $ _ |