The fc Command





The fc Command

The fc command allows you to list, or edit and re-execute history file commands. It is the simplest interface to the command history file provided with the Korn shell. The fc command also allows you to manipulate the history file using your own choice of editors.

Displaying the Command History File

The command history file can be displayed using the fc command in a number of ways: with or without command numbers, using a range of line numbers, in reverse order, and more. The format for displaying the command history file with the fc –l command is:

fc —l[nr] [range] 

where the –n option causes the command numbers to not be displayed, and the –r option specifies reverse order (latest commands first). The range of commands to list is given as:

n1 [n2]

display list from command n1 to command n2. If n2 is not specified, display all the commands from current command back to command n1.

?count

display the last count commands

string

display all the previous commands back to the command that matches

If no range argument is given, the last sixteen commands are listed. Let's look at the last five commands:

$ fc —l ? 
250          set 
251          vi /etc/env 
252          . /etc/env 
253          set 
254          alias 
255          functions 

The history command is equivalent to fc –l. It is much easier to remember than fc –l, especially for C shell users. The last command could also be given like this. Notice that the order is reversed.

$ history —r ? 
255          functions 
254          alias 
253          set 
252          . /etc/env 
251          vi /etc/env 
250          set 

By using a string instead of a count argument, we could search backward for a specific command.

$ history —r set 
258          fc —lr set 
257          fc —ln ?0 
256          fc —l 250 265 
255          functions 
254          alias 
253          set 

The argument set could also be given as s, or se, since partial strings are also matched. This means that the string f would match functions and fc, while fu would only match functions.

Editing the Command History File

Besides displaying the command history file, it can also be edited using the fc command with the following format:

fc [–e editor] [–r] [range] 

or

fc –e ?[old=new] [command] 

where the –e editor option is used to specify an editor. If not given, the value of the FCEDIT variable is used, and if not set, the default / bin/ed is used. The –r option reverses the order of the commands, so that the latest commands are displayed first. The first format allows you to edit a list of commands before re-executing. The range of commands to edit is given as:

n1 n2

edit list from command n1 to command n2

n

edit command n

?/span>n

edit the last nth command

string

edit the previous command that matches string

If no range argument is given, the last command is edited. The second format listed allows you to edit and re-execute a single command, where old=new specifies to replace the string old with new before re-executing, and command specifies which command to match. The command can be given as:

n

edit and re-execute command number n

?/span>n

edit and re-execute the last nth command

string

edit and re-execute the most previous command that matches string

If no command argument is given, the last command is edited. Command 173 could be edited and re-executed like this:

$ fc —e ?173 

Another way to do this is with the r command. It is the same as the fc –e ?/span> command. Using r, the last command could also be given as:

$ r 173 

As you can see, the r command is easier to use (and remember) than the fc ?e ?/span> command. What else can be done with this command? The substitution feature is used to make minor changes to a previous command. Let's start with print Hello:

$ print Hello 
Hello 

We could change Hello to something else like this.

$ r Hello=Goodbye print 
print Goodbye 
Goodbye 

The next section covers an easier way to edit and re-execute commands using the in-line editor.



     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows