Linux and Unix fc and history commands

Quick links

About fc and history
Examples
Related commands
Linux and Unix main page

About fc and history

The fc command lists, edits, or re-executes commands previously entered to a shell. The history command allows you to 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.

Syntax: history

history [-c] [-d offset ] [ n ]
history -anrw [ filename ]
history -ps arg [ arg... ]

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 will help you navigate and use your command history within your 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.

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 FILENAME is given, it is used as the history file. Otherwise, if $HISTFILE has a value, that is 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 time stamp associated with each displayed history entry. No time stamps 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
ed
ksh
sh