fc Command

Purpose

Processes the command history list.

Syntax

To Open an Editor to Modify and Reexecute Previously Entered Commands

fc [ -r ] [ -e Editor ] [ First [ Last ] ]

To Generate a Listing of Previously Entered Commands

fc -l [ -n ] [ -r ] [ First [ Last ] ]

To Generate a Listing of Previously Entered Commands with Time of Execution

fc -t [ -n ] [ -r ] [ First [ Last ] ]

To Re-execute a Previously Entered Command

fc -s [ Old= New ] [ First ]

Description

The fc command displays the contents of your command history file or invokes an editor to modify and reexecutes commands previously entered in the shell.

The command history file lists commands by number. The first number in the list is selected arbitrarily. The relationship of a number to its command does not change except when the user logs in and no other process is accessing the list. In that case, the system resets the numbering to start the oldest retained command at 1.

If the numbers in the command history file reach a limit greater than the value of the HISTSIZE environment variable or 32767, whichever is greater, the shell wraps to 1. Despite this optional number wrapping, the fc command maintains the time-ordering sequence of the commands. For example, if three commands in sequence are given the numbers 32766, 32767, and 1 (wrapped), command 32767 is still considered previous to command 1.

The commands in the history file can be displayed using the -l (lowercase L) flag. When the -l flag is not specified and commands are edited using the -e Editor flag, the resulting lines are entered at the end of the history file and then reexecuted by the shell (the fc -e Editor command is not entered into the command history list). If the editor returns a non-zero exit status, this suppresses entry in the history file and command reexecution.

Any command-line variable assignments or redirection operators used with the fc command again invoke the previous command, suppressing standard error for both the fc command and the previous command. For example:

fc -s -- -1 2>/dev/null

Flags

Item Description
-e Editor Edits commands using the specified editor. The Editor parameter should be a command name. The command is located using the PATH environment variable. The value in the FCEDIT environment variable is used as a default when the -e flag is not specified. If the FCEDIT environment variable is null or unset, the ed editor is used.
-l (lowercase L) Lists the commands in your history file. No editor is invoked to modify them. The commands are written in the sequence indicated by the First and Last parameters, as affected by the -r flag, with each command preceded by the command number.
-n Suppresses command numbers when used with the -l flag.
-r Reverses the order of the commands listed (when used with the -l flag) or reverses the order of the commands edited (when the -l flag is not specified).
-s Reexecutes a command without invoking an editor. If the First parameter is not also specified, the -s flag re-executes the previous command.
-t Lists the commands in your history file along with there time of execution. The working is similar to -l flag but the time of execution of the command is displayed.
Note: If the time field is recorded previously by setting EXTENDED_HISTORY=ON, then formatted time field is displayed, else "?".

Parameters

Item Description
First or Last Selects the commands to list or edit. The number of previous commands that can be accessed is determined by the value of the HISTSIZE environment variable. The First and Last parameters must have one of the following values:
[+] Number
Represents a specific command number. Command numbers can be displayed with the -l flag. A + (plus sign) is the default.
-Number
Represents a command that was previously executed, specified by the number of commands to back up in the history list. For example, -1 indicates the immediately previous command.
String
Indicates the most recently entered command that begins with the specified string. If the Old=New parameter is specified without the -s flag, the string from the First parameter cannot contain an embedded = (equal sign).

When using the -s flag, omission of the First parameter causes the previous command to be used.

When the -s flag is not specified, the following rules apply:

Item Description
Old=New In commands to be reexecuted, replaces the fist occurrence of the old string with the new string.

Environment Variables

The following environment variables affect the execution of the fc command:

Item Description
EXTENDED_HISTORY Used to control the recording of time of command execution in the history file. If the variable is set to ON then the time is recorded, otherwise, it is not recorded.
FCEDIT When expanded by the shell, determines the default value for the -e editor variable. If the FCEDIT environment variable is null or is not set, the ed editor is the default.
HISTDATEFMT This is used to control the format of the time displayed by the fc –t command. For example, if HISTDATEFMT=%Y, then fc -t will display the year when the command is executed. The formatting is similar to that done by date command.
HISTFILE Determines the path name of the command history file. If the HISTFILE environment variable is not set, the shell may attempt to access or create the .sh_history file in the user's home directory.
HISTSIZE Determines a decimal number representing the limit to the number of previous commands that are accessible. If this variable is not set, a default value of128 is used.

Exit Status

The following exit values are returned:

Item Description
0 Successful completion of the listing.
>0 An error occurred.

Otherwise, the exit status is that of the commands executed by the fc command.

Examples

  1. To invoke the editor defined by the FCEDIT environment variable on the most recent command (the default editor is /usr/bin/ed), enter:
    fc
    The command is executed when you finish editing.
  2. To list the previous two commands that were executed, enter:
    fc -l -2
  3. To find the command that starts with cc, change foo to bar, and display and execute the command, enter:
    fc -s foo=bar cc
  4. To list the previously executed commands along with there time of execution, type:
    fc –t

Files

Item Description
/usr/bin/ksh Contains the Korn shell fc built-in command.
/usr/bin/fc Contains the fc command.