|
Other Ways to Access Your HistoryThere are several other ways to access your command history that are worth noting. The history CommandThe 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:
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:
The fc CommandThe 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
writes commands 510 through 515 to standard output, whereas the command
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:
(That's the letter l followed by the number -1.) fc is described in full detail in Appendix A. The r CommandA 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:
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:
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:
or even more simply, you could have typed:
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:
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):
|
|
No comments:
Post a Comment