Shells keep a history of the commands which you have used, and you may refer to this history to reduce the amount of typing which you must do.
Arrow Keys
The ↑ and ↓ keys can be used to navigate through historical commands, though this is someone tedious, requiring the hand to move from the home row to the arrow keys. What follows are some easy ways of accessing and repeating historical commands.
!!
For example, the command !! repeats the last command. Suppose you are editing a file, you save and exit and you then realize you wanted to make another change:
{ecelinux:1} pico SingleList.h {ecelinux:2} !! pico SingleList.h
For your information, the terminal will echo (print) the command which is being executed, in this example, pico SingleList.h. If you intend to run an executable file multiple times, you only need print the name once:
{ecelinux:1} ./a.out {ecelinux:2} !! ./a.out {ecelinux:3} !! ./a.out
history
The history command prints all the commands which have been used and a number corresponding to each command. It also specifies the time the command was entered. You may use history to list all the commands and then you may examine them to determine which command you wish to use (or reuse) and then enter ! followed by the command number:
{ecelinux:1} history . . . 134 3:50 ls 135 3:51 cp *.c ~/backup/ 136 3:51 rm *.c . . . {ecelinux:2} !135 135 cp *.c ~/backup/
!$
The !$ may be used to specify all arguments of the previous command. Thus, the following allows you edit the file myfile.c and then compile it using gcc:
{ecelinux:1} pico myfile.c {ecelinux:1} gcc !$
!<prefix>
If you type ! followed by any number of characters, it will search throughout history for the most recent command which started with those characters. Thus, you should only have to type the full gcc myfile.c once. After that, you may always refer to it with !gc. Similarly, if you are always editing the same file, you may use pico SingleList.h once, and the next time you wish to edit the file, you may use !p.
Summary
This topic covers some of the basic history-manipulation tools which are most commonly used in Unix shells. In a future topic, you will see how to manipuate these commands.
Copyright ©2005-2012 by Douglas Wilhelm Harder. All rights reserved.