The fc Command

fc is a built-in shell command that provides a superset of the C shell history mechanism. You can use it to examine the most recent commands you entered, to edit one or more commands with your favorite “real” editor, and to run old commands with changes without having to type the entire command in again. We’ll look at each of these uses in turn.

The -l option to fc lists previous commands. It takes arguments that refer to commands in the history file. Arguments can be numbers or alphanumeric strings; numbers refer to the commands in the history file, while strings refer to the most recent command beginning with the string. fc treats arguments in a rather complex way:

  • If you give two arguments, they serve as the first and last commands to be shown.

  • If you specify one number argument, only the command with that number is shown.

  • With a single string argument, it searches for the most recent command starting with that string and shows you everything from that command to the most recent command.

  • If you specify no arguments, you will see the last 16 commands you entered. bash also has a built-in command for displaying the history: history.

A few examples should make these options clearer. Let’s say you logged in and entered these commands:

            ls -l
            more myfile
            vi myfile
            wc -l myfile
            pr myfile | lp -h

If you type fc -l with no arguments, you will see the above list with command numbers, as in:

1	ls -l
2	more myfile
3	vi myfile
4	wc -l myfile
5	pr myfile | lp -h

Adding another ...

Get Learning the bash Shell, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.