LinuxCommandLibrary

fc

Edit and re-execute shell commands

TLDR

Open the last command in the default system editor and run it after editing

$ fc
copy

Specify an editor to open with
$ fc -e ['emacs']
copy

List recent commands from history
$ fc -l
copy

List recent commands in reverse order
$ fc -l -r
copy

Edit and run a command from history
$ fc [number]
copy

Edit commands in a given interval and run them
$ fc '[416]' '[420]'
copy

Display help
$ fc --help
copy

SYNOPSIS

fc [-e editor] [-lnr] [first] [last]
fc -s [old=new] [command]

PARAMETERS

-e editor
    Specify Editor. Invokes the specified editor to edit the commands. If editor is '-', no editor is invoked, and the commands are simply re-executed. If omitted, FCEDIT or EDITOR environment variables are used, or vi by default.

-l
    List History. Lists the commands rather than invoking an editor. Commands are printed to standard output.

-n
    Suppress Numbers. Used with -l, suppresses the command numbers when listing history.

-r
    Reverse Order. Reverses the order of commands. When listing (-l), it prints newest commands first. When editing or re-executing, it processes them in reverse chronological order.

-s
    Substitute and Re-execute. Re-executes a command without invoking an editor. An optional old=new substitution can be applied to the command before execution. If command is omitted, the most recent command is used.

first
    Start Range. Specifies the beginning of the command range. Can be a number (positive for absolute history number, negative for offset from current), or a string that matches the most recent command beginning with that string.

last
    End Range. Specifies the end of the command range. Behaves like first. If omitted when first is provided, it defaults to first for editing/re-execution, or the current command for listing.

DESCRIPTION

The fc (fix command) utility is a powerful shell built-in command that allows users to interact with their shell's command history. Its primary function is to invoke an editor on a range of previous commands, enabling easy modification and re-execution without retyping.

Alternatively, fc can simply list previous commands to standard output, providing a quick way to review executed commands. This command is particularly useful for correcting errors in long commands, reusing complex command structures, or quickly executing a slightly modified version of a previous command.

By default, fc uses the editor specified by the FCEDIT environment variable, or EDITOR, falling back to vi if neither is set. When editing, the commands are loaded into the editor, and upon saving and exiting, they are re-executed by the shell. The range of commands can be specified by their history numbers or by a string prefix matching a previous command.

CAVEATS

fc is a shell built-in command, and its exact behavior and available options can vary slightly between different shells (e.g., Bash, Zsh, Ksh). The effectiveness of fc depends on the shell's history mechanism being enabled and properly configured (e.g., HISTSIZE, HISTFILE). The command relies on an external text editor being available on the system. If no editor is specified or found, fc might fail or resort to a default that is not user-friendly.

DEFAULT EDITOR

By default, fc attempts to use the editor specified by the FCEDIT environment variable. If FCEDIT is not set, it falls back to the EDITOR environment variable. If neither is set, it typically defaults to vi. Users can explicitly set FCEDIT in their shell's configuration file (e.g., .bashrc) to their preferred editor (e.g., export FCEDIT=nano).

COMMAND NUMBERING

Command numbers can be positive (absolute history index) or negative (relative to the current command, where -1 is the previous command). For example, fc -e vi -1 edits the last command, and fc -l 10 lists from history entry 10 to the current.

IMPLICIT RANGE

If first is provided but last is not, and fc is used for editing, the range defaults to just first. If fc -l is used, the range defaults from first to the current command. If neither first nor last is provided, fc -l lists the last 16 commands by default, while fc for editing/re-execution defaults to the last command executed.

HISTORY

The fc command originated in the Korn shell (ksh) and was subsequently adopted by other popular shells like Bash and Zsh. Its design was intended to provide a more sophisticated command-line editing and history manipulation capability than previously available. It built upon the simpler history mechanisms found in earlier shells, introducing the concept of invoking an external editor for command modification, making it a powerful tool for interactive shell users. Its inclusion in POSIX standards has ensured its widespread availability and consistent behavior across compliant Unix-like systems.

SEE ALSO

history(1), bash(1), zsh(1), ksh(1)

Copied to clipboard