The fc
command is used to display and manipulate the history list of commands in Linux. It allows you to view, edit, and execute commands from your command history. You can also use this command to re-execute commands from your history list by specifying the command number or a range of commands.
Syntax
The syntax for the fc
command is as follows:
fc [options] [first] [last]
Examples
To view the last 10 commands in your history list, you can use the following command:
fc -l -10
To edit and execute the last command in your history list, you can use the following command:
fc -e -
To execute the command numbered 123 from your history list, you can use the following command:
fc 123 123
To execute the last 3 commands in your history list, you can use the following command:
fc -3 -3
Options
The following options are available for the fc
command:
Option | Description |
---|---|
-e [command] |
Edit the specified command and execute it. If no command is specified, the last command in the history list is edited. |
-l [first] [last] |
List the commands in the history list. If no range is specified, all commands are listed. |
-n [command] |
Execute the specified command without adding it to the history list. |
-r [first] [last] |
Reverse the order of the commands in the history list. If no range is specified, all commands are reversed. |
-s [pattern] [replacement] |
Replace the first occurrence of the specified pattern in the last command and execute it. |
-t [command] |
Execute the specified command and display the time it took to execute. |
Troubleshooting Tips
- If you receive an error message stating that the command cannot be found, ensure that you have entered the correct command number or range.
- If you are unable to edit a command using the
-e
option, check that you have the correct permissions to edit the file.
Notes
- The
fc
command is a built-in command in most Linux distributions and does not require installation. - The
fc
command can be used in conjunction with other Linux commands, such asgrep
andsed
, to manipulate the history list.