|
Softpanorama |
May the source be with you, but remember the KISS principle ;-)
|
The internal fc command provides access to your history file. The Korn shell creates a history file containing the commands you have previously entered during your current login session. You use the fc utility to:
Following is the general format of the ksh version of fc command.
fc [ -e editor ] [ -lnr ] [ first [ last ] ]
fc -e - [ old=new ] [ command ]
history is an alias for fc -l
r is an alias for fc -e -.
The r command is called the repeat command. It searches for a string in the history file that matches the given argument and then executes the entire line in which the string was found.
Options
The following list describes the options and their arguments that may be used to control how fc functions.
| First format | |
|---|---|
| -e editor | Use editor editor to edit the commands in the history file. The editing of the history file is referred to as in-line command editing. If you do not specify the -e option, fc checks the value of the FCEDIT variable for an editor name. If FCEDIT is not defined then /bin/ed is used. Valid editors are vi, emacs, gmacs, and ed. |
| -l | List the contents of the history file. |
| -n | Suppress the command numbers when displaying the commands from the history file. |
| -r | Reverse the order of the commands in the history file when they are listed. |
| -first | Beginning command. The number of lines to go back from the last command to start listing out the history file. For example, |
fc -l -25 # last 25 commands
history -25
| -last | Ending command. The number of lines to go back from the last command to end listing out the history file. For example, |
fc -l -n -25 -5 # 25 commands ago down to 5 commands ago
history -n -25 -5
| Second Format | |
|---|---|
| -e - | Reexecutes the previous command in the history file. If you do not specify a command or partial command, then the last command you executed is re- executed. This command is the r alias (alias r="fc -e -"). |
Arguments
The following list describes the arguments that may be passed to the fc command.
| First Format | |
|---|---|
| first | List commands starting at line first in the history file. For instance, |
fc -l 1 # list all commands (starting from line 1
history 1
| last | List commands up to line last in the history file. For example, |
fc -l 20 30
history 20 30
| displays lines 20 through 30 of the history file. |
| Second Format | |
|---|---|
| old=new | Substitutes old string for new string in the appropriate command before reexecuting it. |
| command | The command to reexecute from the history file. Command may be one of the following: |
fc -e c or
r c searches the history file for the last command that began with a c.
Variables
There are two variables that control the location and size of the history file. The HISTFILE variable informs the shell where to locate the history file. For example,
HISTFILE=$HOME/.history
places the history file in your HOME directory in the .history file. If you do not specify a path in the HISTFILE variable, the default for the location is $HOME/.sh_history.
The HISTSIZE variable is used to set the number of commands that are stored in the history file. If you do not set the HISTSIZE variable to some number it defaults to 128. Therefore, the history file will contain the last 128 commands you have entered.
The FCEDIT variable is used to define the editor used to edit your history file. The possible editors are vi, emacs, gmacs, and ed.
/usr/bin/fc [first[last]]
/usr/bin/fc -l [-nr] [first[last]]
/usr/bin/fc -s [old=new] [first]
fc -e - [old=new]
[command]
fc [-e ename] [-nlr]
[first[last]]
The fc utility lists or edits and reexecutes, commands previously entered to an interactive sh.
The command history list references commands by number. The first number in the list is selected arbitrarily. The relationship of a number to its command will not change except when the user logs in and no other process is accessing the list, at which time the system may reset the numbering to start the oldest retained command at another number (usually 1). When the number reaches the value in HISTSIZE or 128 (whichever is greater), the shell may wrap the numbers, starting the next command with a lower number (usually 1). However, despite this optional wrapping of numbers, fc will maintain the time-ordering sequence of the commands. For example, if four commands in sequence are given the numbers 32 766, 32 767, 1 (wrapped), and 2 as they are executed, command 32 767 is considered the command previous to 1, even though its number is higher.
When commands are edited (when the -l option is not specified), the resulting lines will be entered at the end of the history list and then reexecuted by sh. The fc command that caused the editing will not be entered into the history list. If the editor returns a non-zero exit status, this will suppress the entry into the history list and the command reexecution. Any command-line variable assignments or redirection operators used with fc will affect both the fc command itself as well as the command that results, for example:
fc -s -- -1 2>/dev/null
reinvokes the previous command, suppressing standard error for both fc and the previous command.
History substitution allows you to use words from previous command lines in the command line you are typing. This simplifies spelling corrections and the repetition of complicated commands or arguments. Command lines are saved in the history list, the size of which is controlled by the history variable. The history shell variable may be set to the maximum number of command lines that will be saved in the history file; i.e.:
set history = 200
will allow the history list to keep track of the most recent 200 command lines. If not set, the C shell saves only the most recent command.
A history substitution begins with a ! (although you can change this with the histchars variable) and may occur anywhere on the command line; history substitutions do not nest. The ! can be escaped with \ to suppress its special meaning.
Input lines containing history substitutions are echoed on the terminal after being expanded, but before any other substitutions take place or the command gets executed.
Using fc, in the form of
fc -e - [ old=new ] [ command ],
the command is re-executed after the substitution old=new is performed. If there is not a command argument, the most recent command typed at this terminal is executed.
Using fc in the form of
fc [ -e ename ] [ -nlr ] [ first [ last ] ],
a range of commands from first to last is selected from the last HISTSIZE commands that were typed at the terminal. The arguments first and last may be specified as a number or as a string. A string is used to locate the most recent command starting with the given string. A negative number is used as an offset to the current command number. If the -l flag is selected, the commands are listed on standard output. Otherwise, the editor program -e name is invoked on a file containing these keyboard commands. If ename is not supplied, then the value of the variable FCEDIT (default /bin/ed) is used as the editor. When editing is complete, the edited command(s) is executed. If last is not specified then it will be set to first. If first is not specified the default is the previous command for editing and -16 for listing. The flag -r reverses the order of the commands and the flag -n suppresses command numbers when listing. (See ksh(1) for more about command line editing.)
The text of the last HISTSIZE (default 128) commands entered from a terminal device is saved in a history file. The file $HOME/.sh_history is used if the HISTFILE variable is not set or if the file it names is not writable. A shell can access the commands of all interactive shells which use the same named HISTFILE. The special command fc is used to list or edit a portion of this file. The portion of the file to be edited or listed can be selected by number or by giving the first character or characters of the command. A single command or range of commands can be specified. If you do not specify an editor program as an argument to fc then the value of the variable FCEDIT is used. If FCEDIT is not defined then /bin/ed is used. The edited command(s) is printed and re-executed upon leaving the editor. The editor name - is used to skip the editing phase and to re-execute the command. In this case a substitution parameter of the form old=new can be used to modify the command before execution. For example, if r is aliased to ´fc -e - ´ then typing `r bad=good c' will re-execute the most recent command which starts with the letter c, replacing the first occurrence of the string bad with the string good.
Using the fc built-in command within a compound command will cause the whole command to disappear from the history file.
The following options are supported:
The following operands are supported:
When the synopsis form with -s is used:
For the synopsis forms without -s :
fc -r 10 20 fc 30 40 fc 20 10 fc -r 40 30
fc -l fc 1 99
will list and edit, respectively, all ten commands.
When the -l option is used to list commands, the format of each command in the list is as follows:
"%d\t%s\n", <line number>, <command>
If both the -l and -n options are specified, the format of each command is:
"\t%s\n", <command>
If the commandcommand consists of more than one line, the lines after the first are displayed as:
"\t%s\n", <continued-command>
csh ksh % history $ fc -l 1 cd /etc 1 cd /etc 2 vi passwd 2 vi passwd 3 date 3 date 4 cd 4 cd 5 du . 5 du . 6 ls -t 6 ls -t 7 history 7 fc -l % !d $ fc -e - d du . du . 262 ./SCCS 262 ./SCCS 336 . 336 . % !da $ fc -e - da Thu Jul 21 17:29:56 PDT 1994 Thu Jul 21 17:29:56 PDT 1994 % $ alias \!='fc -e -' % !! $ ! date alias ='fc -e -' Thu Jul 21 17:29:56 PDT 1994
See environ(5) for descriptions of the following environment variables that affect the execution of fc: LC_CTYPE, LC_MESSAGES, and NLSPATH.
The following exit values are returned:
Otherwise, the exit status will be that of the commands executed by fc.
Copyright © 1996-2008 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
Standard disclaimer: The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.
Last modified: February 28, 2008