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.

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.

If you’re looking for the history of the Linux operating system, see the history of Unix-like operating systems.

  • Description
  • Syntax: history
  • Syntax: fc
  • Other history commands and shortcuts
  • Examples
  • Related commands
  • Linux commands help

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

If FILE NAME 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 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

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

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:

!ls

!!

Would re-execute the most recently executed command.

csh — The C shell command interpreter.ed — A simple text editor.ksh — The Korn shell command interpreter.sh — The Bourne shell command interpreter.