Linux fc and history commands
In the bash shell, the fc built-in command lists, edits, or re-executes commands previously entered to a shell. The history built-in command lets you use words from previous command lines in the command line you are typing. This simplifies spelling corrections and the repetition of complicated commands or arguments.
If you're looking for the history of the Linux operating system, see the history of Unix-like operating systems.
Description
Each shell (the Bourne shell, the Bourne Again Shell, the C Shell, the Korn Shell, etc.) has its own slight differences in how it handles, and allows access to, the command history. In general, the following commands help you navigate and use your command history in the Linux/Unix shell.
history displays or manipulate the history list with line numbers, prefixing each modified entry with a '*'. An argument of n lists only the last b entries.
Syntax: history
history [-c] [-d offset ] [ n ]
history -anrw [ file name ]
history -ps arg [ arg... ]
Options: history
-c | Clear the history list by deleting all of the entries. |
-d offset | Delete the history entry at offset OFFSET. |
-a | Append history lines from this session to the history file. |
-n | Read all history lines not already read from the history file. |
-r | Read the history file and append the contents to the history list. |
-w | Write the current history to the history file and append them to the history list |
-p | Perform history expansion on each ARG and display the result without storing it in the history list. |
-s | Append the ARGs to the history list as a single entry. |
If FILE NAME is given, it is used as the history file. Otherwise, if $HISTFILE has a value, that's used, else ~/.bash_history.
If the $HISTTIMEFORMAT variable is set and not null, its value is used as a format string for strftime to print the timestamp associated with each displayed history entry. No timestamps are printed otherwise.
Syntax: fc
fc [-e ename] [-lnr] [first] [last]
fc -s [pat=rep] [command]
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: fc
-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.
Other history commands and shortcuts
!string | Execute the most recent command that begins with string. |
!num | Execute command that is number num in the command history. |
!-num | Execute the command was run num commands previous in the history. |
!! | Execute the previous (most recently-executed) command. |
!?string[?] | Execute the most recent command containing the string string. The trailing ? may be omitted if string represents the end of the command in question. |
^string1^string2^ | Repeat the previous command executed, replacing string1 with string2. The previous command must contain string1. |
Examples
fc -l
Lists the history of commands on the computer similar to the following:
2 grep --help 3 bg 4 fg 5 pine 6 cd public_html 7 rm index.html 8 sz index.html 9 ls -laxo 10 chmod 755 index.htm
fc -e - ls
Executes the most recently executed command that begins with the letters ls.
history
Typing history alone would give results similar to the following:
2 grep --help 3 bg 4 fg 5 pine 6 cd public_html 7 rm index.html 8 sz index.html 9 ls -laxo 10 chmod 755 index.htm
!ls
Executes the most recently executed command that begins with the letters ls.
!!
Would re-execute the most recently executed command.
Related commands
csh — The C shell command interpreter.
ed — A simple text editor.
ksh — The Korn shell command interpreter.
sh — The Bourne shell command interpreter.