181 ls 182 ls 183 cat /etc/shells 184 cat /etc/hosts 185 wThen you can run the command 183 again as follows:
[amit@onyx]: !183 cat /etc/shells /bin/bash /bin/sh /bin/ash /bin/bsh /bin/bash2 /bin/tcsh /bin/csh /bin/ksh /bin/zsh [amit@onyx]:
Alternately, we can use ! followed by a prefix of the command and the shell searches for and executes the last command that started with the given prefix. In the above example, saying !cat, will result in the command cat /etc/hosts to be executed.
Typing two bang characters is a short-cut for repeating the last command.
[amit@onyx]: date Mon Oct 25 14:29:19 MDT 2012 [amit@onyx]:!! date Mon Oct 25 14:29:23 MDT 2012 [amit@onyx]:
You can go to the previous command using the built-in editor mode. The default mode setup in your .bash_profile file is vi editor mode.
You can use the arrow keys ( and ) to go up and down the list of commands that you typed into the shell until you reach the desired command. You can use backspace and arrow keys ( and ) to edit the command. Press the RETURN key to execute the command.
If you wish to use the more powerful editing commands from vi, type in the ESC key. Now you can use the vi search command /string to search for a previous command containing that string. Once you get to the desired command you can edit it further using the standard vi editing commands. After you are done editing just type Return to execute the command.
You can also set the editor mode for the bash shell to be Emacs
by placing the following command in your .bash_profile file.
set -o emacs
Another common technique is to grep through the history to find the command you had typed earlier.
[amit@onyx C-examples]: history | grep javac 8202 javac 8206 javac WebStats.java 8211 javac -O WebStats.java 10082 history | grep javac [amit@onyx C-examples]:
Here the vertical bar symbol | is the pipe symbol that connects the two commands history and grep together. This is an example of object composition!
Under X Windows you can use the mouse to cut and paste previous commands. See Section 3.8.