Project49.Use Command-Line Recall


Project 49. Use Command-Line Recall

"How can I quickly reissue a command I issued a while back?"

This project shows you several ways to recall a previously issued command from the command-line history and how you might edit that command after it has been recalled. Project 48 covers the history mechanism itself.

Search History

Project 4 showed you how to recall previously issued commands by pressing the up arrow (Cursor-Up) key to move back through the command-line history. By typing Escape <, we can move straight to the first line in the history and press the down arrow (Cursor-Down) key to move forward through the history. Typing Escape > moves us straight to the end of the history.

Bash also provides several mechanisms to recall previous commands by searching the history. The first mechanism is called an incremental search. To instigate such a search, press Control-r and then start typing a command. As you type, the history will be reverse-searched for the first line that matches what you have typed so far. For example, press Control-r and then type ls followed by a space. Assuming that you have issued such a command within recorded history, Bash will complete the command line for you.

$ Control-r (reverse-i-search)`': (reverse-i-search)`ls ~/D ': ls ~/Documents/


If the command shown is not the one you are looking for, either type more characters to identify it uniquely or press Control-r repeatedly until the desired command is found. Each press of Control-r causes Bash to search farther back in history for a matching command.

(reverse-i-search)` ls ~/De ': ls ~/Desktop/


Press Return to execute the command. Alternatively, press Escape to edit the command, from where you could also press Control-k to delete the line.

Tip

It's not necessary to type the first letters of the command you wish to search for. Pressing Control-r and typing Doc will search for any command that contains Doc, not just those that begin with Doc.


You initiate a nonincremental search by typing Escape p. Unlike an incremental search, it does not happen as you type, but when you press Return. Suppose that we wish to recall the tail -f command line shown in the history listing below.

$ history   ...   502 tail -f /var/log/system.log   503 ls -al   504 tab2space ~/Documents/letter.txt   505 history


Let's recall it by pressing Escape p and then typing ta.

$ <Escape>p $ :ta<Return>


Pressing Return initiates the search.

$ tab2space ~/Documents/letter.txt


If the recalled line is not the one we wanted, we type Escape p and then press Return again.

History Expansion

History expansion uses the exclamation mark (!) to recall a previous line. Here's an example of what history expansion offers. Suppose that we have the following command-line history.

$ history ...    6 du    7 echo "Hello"    8 exit    9 history


To recall the last line (history), type

$ !!


To recall line 7 (echo "Hello") type

$ !7


Learn More

Project 45 covers Bash shell options and attributes in general, showing you how to display them and switch them on and off.


To recall three lines back, type

$ !-3


To search backward for the first line that starts with "ec', type

$ !ec


Verify First

A command that has been recalled via history substitution is immediately executed. If we enable the shell option histverify, Bash will display the recalled line first, waiting for Return to be pressed before executing it.

Let's enable history verify and search backward for the first command containing the string "He".

$ shopt -s histverify $ !?He? $ echo "Hello"<Return>


Notice that in this example, we typed !?He? to search for a command that contains the string "He", whereas previously, we typed !ec to search for a command that starts with the string "ec".

Recall Arguments

Should you wish to reuse an argument from a previously issued command, placing it in situ on the current command line, employ the following technique. Suppose that we issued the command

$ cat a-big-filename-one a-big-filename-two I am big file one I am big file two


Now we wish to edit with nano the file a-big-filename-two. We note that it was the second argument, which we denote with :2, of the last-issued command, which is recalled with !!, as shown earlier in this project. Putting this together, we type

$ nano !!:2


Tip

Type !!* to recall all arguments.


Bash echoes the expanded command line, which we can edit, or we can simply press Return to execute it as is.

$ nano a-big-filename-two<Return> ...


Sometime later, we might want to edit the file a-big-filename-one. To recall the filename to the current command line, we search back for the command that mentioned it and then recall argument one. The most recent command was cat, which we recall by typing !cat, and we recall argument one by typing :1. The command to type, therefore, is

$ nano !cat:1


Tip

There's lots more to history expansion than mentioned here. Read the Bash man page and search for HISTORY EXPANSION by typing /^HISTORY EXP within the man page.


Again, Bash echoes the expanded command line, and we press Return to accept it.

$ nano a-big-filename-one<Return>


Edit the Last Command Line

Bash gives us a way of making a quick substitution-style change to the last command line entered. Suppose that we misspell a-big-filename-one.

$ cat a-bug-filename-one cat: a-bug-filename-one: No such file or directory


We correct bug to big by typing

$ ^bug^big cat a-big-filename-one<Return> I am big filename one





Mac OS X UNIX 101 Byte-Sized Projects
Mac OS X Unix 101 Byte-Sized Projects
ISBN: 0321374118
EAN: 2147483647
Year: 2003
Pages: 153
Authors: Adrian Mayo

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net