Bash - fc (Fix Command) - Range of command from history - (builtin)

> Procedural Languages > Bash Shell and (Unix|Linux) Utilities (XCU)

1 - About

Fix Command (fc) is a builtin command that retrieve a range of commands from first to last is selected from the history list.

Advertising

3 - Syntax

3.1 - First form

fc has two modes:

  • editing (default).
  • listing (-l option)
fc [-e ename] [-nlr] [first] [last]

where:

  • first and last define the range. See first and last
  • -n: No number. It suppresses the command numbers when listing.
  • -r: Reverse order: option reverses the order of the commands.
  • -l: Listing: The commands are listed on standard output.
  • ename: editor name. Edit the file in this editor if the -l option is not given. See editor

If neither variable is set, is used. When editing is complete, the edited commands are echoed and executed.

3.1.1 - First and last

  • first and last may be specified as:
    • a string (to locate the last command beginning with that string)
    • or as a number (an index into the history list, where a negative number is used as an offset from the current command number).
  • If last is not specified it is set to the current command for listing (so that fc -l -10 prints the last 10 commands) and to first otherwise.
  • If first is not specified it is set to the previous command for editing and -16 for listing.
Advertising

3.1.2 - Editor

If ename is not given, the value of the FCEDIT variable is used, and the value of EDITOR if FCEDIT is not set.

see Sh - Editor

3.1.3 - Example

  • fc -l -10 prints the last 10 commands

3.2 - Second form

Command is re-executed after each instance of pat is replaced by rep.

fc -s [pat=rep] [cmd]

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 com- mand.

If the first form is used, the return value is 0 unless an invalid option is encountered or first or last specify history lines out of range. If the -e option is supplied, the return value is the value of the last command executed or failure if an error occurs with the temporary file of commands. If the second form is used, the return status is that of the command re-exe- cuted, unless cmd does not specify a valid history line, in which case fc returns failure.

4 - Documentation / Reference