#Bash 的 fc 命令
fc [OPTION]
功能
显示、编辑或执行历史命令。
fc(fix command) 用于列出、编辑和重新执行历史记录列表中的命令。FIRST 和 LAST 可以是指定范围的数字,也可以是字符串,表示以该字符串开头的最近命令。
类型
Bash 内置命令。
参数
OPTION选项:-e NAME- 选择编辑器;默认为FCEDIT-l [FIRST] [LAST]- 显示历史命令;默认为编辑历史命令FIRST- 要操作的第一条命令;行号或前缀LAST- 要操作的最后一条命令;行号或前缀
-n- 不显示行号-r- 逆序输出-s [PAT=REP] [CMD]- 将历史命令CMD(行号或前缀)中的PAT替换为REP然后执行;默认执行上一条命令
#示例
查看历史命令
$ echo 1
1
$ echo 2
2
$ echo 3
3
$ fc -l # 查看历史命令
...
2009 echo 1
2010 echo 2
2011 echo 3
$ fc -l 1995 2000 # 查看指定范围的历史命令
1995 echo 1
1996 echo 2
1997 history -d -2
1998 history
1999 help
2000 help fc
执行历史命令
$ ls /tmp
tmp.XG4RL3jbgF tmp.Urtrw97emL tmp.M8ln2SXW4p
$ fc -s # 重新执行上一条命令
tmp.XG4RL3jbgF tmp.Urtrw97emL tmp.M8ln2SXW4p
$ fc -s tmp=home # 将 tmp 替换为 home 执行
user1 user2 user3
编辑历史命令
$ fc 1995 2000 # 修改行号 1995 至 2000 的历史命令
GNU nano 7.2 /tmp/bash-fc.kxvj5L
echo 1
echo 2
history -d -2
history
help
help fc
[ Read 6 lines ]
^G Help ^O Write Out ^W Where Is ^K Cut ^T Execute ^C Location
^X Exit ^R Read File ^\ Replace ^U Paste ^J Justify ^/ Go To Line
#相关命令
| 命令 | 说明 |
|---|---|
| history | 查看历史命令 |
#推荐阅读
#手册
fc: fc [-e ename] [-lnr] [first] [last] or fc -s [pat=rep] [command]
Display or execute commands from the history list.
fc is used to list or edit and re-execute commands from the history list.
FIRST and LAST can be numbers specifying the range, or FIRST can be a
string, which means the most recent command beginning with that
string.
Options:
-e ENAME select which editor to use. Default is FCEDIT, then EDITOR,
then vi
-l list lines instead of editing
-n omit line numbers when listing
-r reverse the order of the lines (newest listed first)
With the `fc -s [pat=rep ...] [command]' format, COMMAND is
re-executed after the substitution OLD=NEW is performed.
A useful alias to use with this is r='fc -s', so that typing `r cc'
runs the last command beginning with `cc' and typing `r' re-executes
the last command.
Exit Status:
Returns success or status of executed command; non-zero if an error occurs.