TrumanWong

fc

Display the commands in the history list or modify the specified history command and execute it.

Summary

fc [-e ename] [-lnr] [first] [last]
fc -s [pat=rep] [command]

The main purpose

  • Show commands in history list.

  • Edit and re-execute commands from the history list.

Options

-e ename Select the editor to use. The default calling order is environment variable `FCEDIT`, environment variable `EDITOR`, and `vi`.
-l List instead of edit.
-n does not output line numbers when listing (requires -l option).
-r Lists commands in reverse order, with the most recently executed ones listed first (-l option required).
-s [pat=rep] [command] command (the last command executed when not specified) will be re-executed after replacing pat with rep.

Parameters

first: optional; can be a string (the latest command starting with this string), a number (history list index, a negative number represents the offset of the current command number); if not specified, it is set to the previous command and the offset is - 16 (last 16 commands).

last: optional; can be a string (the latest command starting with this string) or a number (history list index, a negative number represents the offset of the current command number); if not specified, it is set to parameter first.

return value

Returns the status of success or execution of the command, or a non-zero value when an error occurs.

example

Replace command parameters:

# List ~ directories
ls ~
# Replace ~ with / and list the root directory after replacement.
fc -s ~=/

Display the 10 most recently used historical commands:

[root@localhost ~]# fc -l -10
1039 type-a grep
1040 export
1041 history 10
1042ulimit-a
1043 shopt
1044 helpls
1045 help env
1046 help short
1047 helpshopt
1048 showkey -a

Edit the 1040th historical command:

[root@localhost ~]# fc 1040

Notice

  1. After closing the terminal, the history list will be written to the history file ~/.bash_history.
  2. The value of the environment variable FCEDIT is the default editor of fc.
  3. This command is a built-in bash command. For related help information, please see the help command.