Home > Environment & Utility > fc

fc: Edit and Rerun Shell History Commands

The `fc` command is used to edit or rerun commands from the shell's history list. It is particularly useful for modifying and re-executing complex commands that were previously run, and can also be utilized when writing shell scripts.

Overview

`fc` stands for 'fix command' and helps you select a command from your shell history to open in your default editor or to rerun immediately without editing. This can save time during repetitive tasks or when correcting typos.

Key Features

  • Edit shell history commands
  • Rerun history commands without editing
  • Select and process specific ranges of commands
  • Specify a default editor

Key Options

The `fc` command offers various options to control how history is processed.

Behavior Control

Generated command:

Try combining the commands.

Description:

`fc` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Learn how to manage history and rerun commands through various examples of the `fc` command.

Edit and Rerun the Most Recent Command

fc

Opens the most recently executed command in the default editor (usually `vi`). After editing and saving, the command will be re-executed.

Edit Recent Command with a Specific Editor

fc -e nano

Uses the `nano` editor to edit and rerun the most recent command.

View the Last 5 Commands

fc -l -5

Displays a list of the last 5 executed commands with their numbers.

Edit a Specific Range of Commands

fc 100 105

Opens commands from history numbers 100 to 105 in the editor for modification and re-execution.

Rerun Previous Command (Without Editing)

fc -s

Immediately re-executes the most recently executed command without invoking an editor. The `-s` option suppresses editor invocation.

Rerun Command Starting with a Specific String

fc -s ssh

Finds and re-executes the most recent command starting with `ssh` without editing.

Substitute String in Previous Command and Rerun

fc -s old_string=new_string

Re-executes the most recent command after changing 'old_string' to 'new_string'. This is similar to the `^old_string^new_string^` shortcut.

Tips & Precautions

Tips and precautions to help you use the `fc` command more efficiently and safely.

Setting the Default Editor

You can specify the default editor that `fc` will use by setting the environment variables `$FCEDIT` or `$EDITOR`.

  • Example: export FCEDIT=nano
  • Priority: $FCEDIT takes precedence over $EDITOR.

Checking History Numbers

You can use the `history` command to view the current shell's history list and the number of each command. This is useful when specifying a particular command with `fc`.

Safe Re-execution

When re-executing complex commands, especially those involving pipes (|) or redirection (>, >>), it is safer to first open the command in `fc`'s editor to review and modify it. Be cautious with `fc -s` as it executes immediately without an editing step.


Same category commands