Sunday, October 25, 2009

Other Ways to Access Your History








 

 










Other Ways to Access Your History



There are several other ways to access your command history that are worth noting.



The history Command



The operation of the history command differs between the Korn shell and Bash because it is not part of the POSIX standard.



The Korn shell history command writes your last 16 commands to standard output:





$ history

507 cd shell

508 cd ch15

509 vi int

510 ps

511 echo $HISTSIZE

512 cat $ENV

513 cp int int.sv

514 history

515 exit

516 cd shell

517 cd ch16

518 vi all

519 run -n5 all

520 ps

521 lpr all.out

522 history



The numbers to the left are simply relative command numbers (command number 1 would be the first, or oldest, command in your history).



Without any arguments, the Bash history command lists your entire history (as specified by the HISTSIZE variable) to standard output. If you just want to see the last few commands, you must specify the number of commands to display as an argument:





$ history 10

513 cp int int.sv

514 history

515 exit

516 cd shell

517 cd ch16

518 vi all

519 run -n5 all

520 ps

521 lpr all.out

522 history 10

$



The fc Command



The fc command allows you to start up an editor on one or more commands from your history or to simply write a list of history commands to your terminal. In the latter form, which is indicated by giving the -l option to fc, it is like typing in history, only more flexible (you can specify a range of commands to be listed or can get fewer or more than the last 16 commands listed). For example, the command





fc -l 510 515



writes commands 510 through 515 to standard output, whereas the command





fc -n -l -20



writes the last 20 commands to standard output, not preceded by line numbers (-n). Suppose that you've just executed a long command line and then decide that it would be nice to turn that command line into a shell program called runx. You can use fc to get the command from your history and I/O redirection to write that command to a file:





fc -n -l -1 > runx



(That's the letter l followed by the number -1.) fc is described in full detail in Appendix A.



The r Command



A simple Korn shell command allows you to re-execute previous commands using even a fewer number of keystrokes than described. If you simply type in the r command, the Korn shell re-executes your last command:





$ date

Thu Oct 24 14:24:48 EST 2002

$ r Re-execute previous command

date

Thu Oct 24 14:25:13 EST 2002

$



When you type in the r command, the Korn shell redisplays the previous command and then immediately executes it.



If you give the r command the name of a command as an argument, the Korn shell re-executes the most recent command line from your history that begins with the specified argument:





$ cat docs/planA

...

$ pwd

/users/steve

$ r cat Rerun last cat command

cat docs/planA

$



Once again, the Korn shell redisplays the command line from its history before automatically re-executing it.



The final form of the r command allows you to substitute the first occurrence of one string with the next. To re-execute the last cat command on the file planB instead of planA, you could type:





$ r cat planA=planB

cat docs/planB

...

$



or even more simply, you could have typed:





$ r cat A=B

cat docs/planB

...

$



Bash has the ! built-in command; !! re-executes the previous command, and !string re-executes the most recent command line from your history that begins with string:





$ !!

cat docs/planB

...

$ !d

date

Thu Oct 24 14:39:40 EST 2002

$



Note that no spaces can exist between ! and string.



The fc command can be used with the �s option to do the same thing with any POSIX-compliant shell (the r command is actually an alias to the fc command in the Korn shell�more on that later in this chapter):





$ fc �s cat

cat docs/planB

...

$ fc �s B=C

cat docs/planC

...

$












     

     


    No comments: