- Unix Commands Reference
- Unix Commands - Home
fc Command in Linux
The fc command in Linux lists, edits, and re-executes the previously executed commands. It is a handy tool to re-execute long commands with multiple parameters. It opens the previously executed commands in an editor, allowing you to edit the command, and when you close the editor after making the necessary changes, the command re-executes.
It is important to note that the fc command interacts with the command history, and each command is assigned a unique number in the command history. The command number wraps around when it reaches the limit HISTSIZE, but the fc command maintains the time order of the commands.
Table of Contents
Here is a comprehensive guide to the options available with the fc command −
Syntax of fc Command
The syntax of the Linux fc command is as follows −
fc [options] [first [last]]
The [options] field is used to specify the options to modify the command's behavior. The [first [last]] is used to specify the command number from the history or range of command numbers.
fc Command Options
The options for the Linux fc command are listed in the following table −
Options | Description |
---|---|
-e editor | It is used to mention the editor’s name (Default is FCEDIT, then EDITOR, and vi) |
-l | It is used to list the commands by the sequence of first and last operands |
-n | It is used to suppress the command numbers when listing them with -l |
-r | It is used to reverse the order of the command list |
-s | It is used to re-execute the command without opening it in the editor |
Operands used with the fc command are listed below −
Operands | Description |
---|---|
first, last | To select a range of commands to list, edit, or execute |
[+]number | To list, edit, or re-execute the command by the command number (1 indicates the first command from the command history) |
-number | To list, edit, or re-execute the command by its command number (-1 indicates the most recently executed command from the command history) |
string | To list, edit or re-execute the most recent command started with the mentioned string |
Examples of fc Command in Linux
This section demonstrates the usage of the fc command in Linux through various examples −
- Editing and Re-executing the Last Executed Command
- Editing and Re-executing a Range of Last Executed Commands
- Editing and Re-executing a Specific Command
- Editing and Re-executing a Command using String
- Re-executing the Command without Editing
- Listing the Commands
- Editing and Re-executing a Last Executed Command in a Specific Editor
Editing and Re-executing the Last Executed Command
To edit and re-execute the last executed command, run the fc command without any option −
fc
The command will open in an editor, modify it, and exit the editor by pressing the ctrl+x keys. Once the editor is closed the command will re-execute.
Note − If the editor is vi it will exit using the :q command. In the vi editor, command editing will be done in insert mode.
Editing and Re-executing a Range of Last Executed Commands
To edit and re-execute a range of last executed commands, the first and last operands will be used −
fc 12 16
The above command will open the commands numbered 12 to 16 inclusively from the command history in the editor, as shown in the output image above. Once you close the editor all 5 commands will be executed sequentially.
Editing and Re-executing a Specific Command
To edit and re-execute the specific command, use number and -number operands. To execute the command number 40 from the beginning of the command list, use the fc command in the following way −
fc 40
To re-execute the command number 40 from the end of the command history, use −
fc -40
Note that instead of command numbers, letters can also be specified. For example, to re-execute a most recent command that starts with the letter p, use −
fc p
If a command starts with the letter p and is present in the last 16 executed commands, it will be executed. Otherwise, a no command found error will be printed.
Editing and Re-executing a Command using String
To re-execute a command using a specific string, the string operand will be used. For example, to edit and re-execute a command that starts with echo, use −
fc "echo"
The command will be opened in the editor if it starts with an echo and is available in the history of the last 16 commands.
Re-executing the Command without Editing
To re-execute the previously executed command without opening it in a text editor, use the -s option. For example, to re-execute the command number 307 without editing it, use −
fc -s 307
In the same way, other operands can also be employed.
Listing the Commands
To list the previously executed commands, use the -l option −
fc -l
By default, the above command will list only the last 16 commands. However, it can be changed using the -number operand. For example, to list 20 commands, use −
fc -l -20
Similarly, to list a range of commands, use the first and last operands −
fc -l 310 314
To reverse the order of command listing, the -r option is used −
fc -rl
By default, the commands are listed with the command numbers. To omit the command numbers, use the -n option −
fc -nl
Editing and Re-executing a Last Executed Command in a Specific Editor
To edit the command in a different editor, use the -e option. For example, to open the last executed command in the vi editor, use the following command −
fc -e vi
Edit the command in insert mode by pressing i, and exit the editor using Esc and :q.
Conclusion
The fc command in Linux is used to list, edit, and re-execute the previously executed commands. It interacts with the shell command history. It is a useful tool to re-execute long and hard-to-remember commands. Moreover, it can also come in handy in a situation where an incorrectly spelled command is executed.
In this tutorial, we explained the Linux fc command, its syntax, options, and usage in Linux through various examples.