How to Use the 'fc' Command (with examples)
The fc
command is a powerful utility in Unix-based systems that allows users to edit and run previous commands from the command history. Originally standing for “fix command,” fc
provides a quick way to correct or adjust commands without having to retype them completely. By opening the last executed command, or a range of commands, in your preferred text editor, you can make necessary changes and re-execute them efficiently. This capability is especially useful for frequent command-line users who want to streamline their workflow and avoid repetitive typing.
Use case 1: Open the last command in the default system editor and run it after editing
Code:
fc
Motivation: Sometimes, the last command executed may have an error or require a modification. By using fc
, you can open the previous command directly in the system’s default text editor, make the necessary changes, and execute it again. This functionality is incredibly useful when dealing with lengthy or complex commands that are prone to typos or syntax mistakes.
Explanation: The fc
command without any options will fetch the most recent command from the history, open it in the default text editor for modification, and then execute it once editing is complete. This saves time by eliminating the need to manually retype the entire command.
Example Output: After executing fc
, the text editor opens with the last command:
nano opened with command: `echo "Hello world!"`
# Change to: `echo "Hello, universe!"`
Once the text editor is saved and closed, the command echo "Hello, universe!"
is executed.
Use case 2: Specify an editor to open with
Code:
fc -e 'emacs'
Motivation: Not all users prefer the default system editor for text modification. You might be more comfortable with a specific editor that offers additional features or a more user-friendly interface, such as emacs
. This option allows for a personalized editing environment to manage commands from history.
Explanation: By using the -e
flag with fc
, you specify which text editor you want to use. In this example, ’emacs’ is specified, so the last command will open in Emacs instead of the default editor.
Example Output: After running the command, Emacs opens with the last command:
Emacs opened with command: `ls -la`
# Modify if needed, save, and close the editor.
The modified command is executed after closing Emacs.
Use case 3: List recent commands from history
Code:
fc -l
Motivation: It is often helpful to see a list of recently used commands to either pick one for another execution or review past activities on the terminal. This functionality can assist in recalling past work sessions or in creating scripts based on command history.
Explanation: The -l
option with the fc
command lists the recent commands from the history. By default, it prints the last few commands executed, typically up to a certain number determined by system settings or user preference.
Example Output: The output provides a list of recent commands:
672 cd /var/www
673 ls
674 mkdir new-directory
Use case 4: List recent commands in reverse order
Code:
fc -l -r
Motivation: Reviewing the command history in reverse can be useful for analyzing the most recent commands first, especially when troubleshooting or auditing actions in a specific sequence. This allows users to backtrack quickly through their terminal activity.
Explanation: The -r
option with fc -l
reverses the order of the displayed command history, making it easy to start with the most recent command and work backward.
Example Output: Commands are listed starting with the most recent:
674 mkdir new-directory
673 ls
672 cd /var/www
Use case 5: Edit and run a command from history
Code:
fc number
Motivation: There are occasions when a specific past command requires alteration or re-execution, such as running a complex command with slight parameter changes. Identifying the exact command by its number in history allows precise modification and execution.
Explanation: By providing a command number to fc
, you retrieve the corresponding command from history, open it in the editor for modifications, and execute the edited command afterward. This is an effective way to target a specific past command without manual text entry.
Example Output: Suppose command number 670
is to be edited:
Emacs opened with command 670: `cp file1.txt /backup/`
# Modify to: `cp file2.txt /backup/`
Upon closing the editor, the fc
command executes the altered command.
Use case 6: Edit commands in a given interval and run them
Code:
fc '416' '420'
Motivation: Sometimes, a sequence of commands needs to be reviewed or altered due to a change in a process or error discovered after execution. By specifying a range, you can edit and execute multiple commands efficiently.
Explanation: The command fc '416' '420'
opens all commands between numbers 416 and 420 for editing in the specified text editor. After editing, these commands are executed in sequence. This allows for batch adjustments and execution of past command sequences.
Example Output: Assuming the commands involve setting and exporting environment variables, the range would open like this:
nano opened with:
# 416 export PATH=$PATH:/newpath
# 417 source .bashrc
# 418 some_other_command
# Modify the necessary commands, save, and close.
Upon closing, the modified sequence of commands is executed.
Use case 7: Display help
Code:
fc --help
Motivation: Understanding the options and syntax available with the fc
command is crucial for effective use. This command provides a detailed list of all flags and capabilities, facilitating better command-line navigation and usage comprehension.
Explanation: By typing fc --help
, users access a summary of command options and functionalities, directly from the terminal, offering guidance and usage examples without external resources.
Example Output: The help manual appears, providing detailed usage instructions:
Usage: fc [options] [first] [last]
...
-e [editor] Select editor to edit command
-l [first] [last] List commands
...
Conclusion:
The fc
command is a versatile tool for managing command history on Unix-based systems, offering profound flexibility for editing and re-executing past commands. As illustrated, it empowers users with the capability to make quick corrections, review command usage, and streamline specific actions efficiently. Whether editing single commands or multiple entries at once, fc
ensures an enhanced and productive command-line experience.