Beginning Fedora 2

The Bash shell comes with numerous special keys and clever shortcuts, which are designed to make your life at the keyboard faster and more efficient. Some of these control process execution, while others simply make it easier to enter your next command.

Control Keystrokes

Many of the following keystrokes are used to interrupt the execution of a process:

Cursor Keys

The cursor keys are those that allow you to manipulate the shell s input cursor. Using the Up and Down arrow keys, it is possible to scroll through a history of commands that were executed, and execute them again (the Up arrow key allows you to scroll backward through the history of commands, while the Down arrow key allows you to scroll forward through them).

Often, you want to execute a command that is only slightly different from a previous command. In this case, you can use the Up and Down arrow keys to retrieve the previous command, and then use a number of different keys to position the cursor and edit the command:

The history Command

While we re on the subject of the command history, it s worth taking a look at the history command. The history command prints a history of the commands executed, with a sequential number next to each command in the history:

$ history 1 ls -al 2 date 3 pwd 4 who 5 touch cellar/maze/labyrinth/passage/foo.txt $ !2

date

Tue Jan 14 23:03:43 2003 $

A previous command at position n can be referred to as !n . So, to execute the date command again, you could simply type in !2 (as shown in the example). While this does not sound like much of a saving, it is a great help when you re working with very lengthy commands.

Other Related Keyboard Shortcuts from the Shell s History

Here are some more useful shortcuts that can be used to invoke commands from the history:

Auto-Completion

The previous section looked at shortcuts that help to quickly ferret out commands from the history and execute them as such or modify them before executing them. This section explores shortcuts to reference commands and objects in the filesystem. The Tab key is your ally in this quest.

Command Completion

To appreciate command completion, you first need to understand how, when you type in a command at the prompt, the shell knows where to look for that command on the filesystem. For example, when you type in the date command, the shell seems to know that you are referring to the date program residing in the /bin directory.

In fact, when Bash starts up, it consults a startup script . The startup script contains an environment variable called PATH , the value of which is a list of directories that contain programs. Then, when you type in a command and press Enter , Bash automatically searches the directories in this list, in an attempt to locate a program whose name is the same as the command you typed in.

Note

We ll discuss environment variables and startup variables more as we progress through the chapter.

If you type in just the first few letters of a command at the Bash prompt, and then press the Tab key, Bash starts searching the PATH (the list of directories) for a unique command that matches the starting letters that you typed in:

Filename Completion

Apart from automatically matching and completing the commands to be executed, Bash also features automatic completion of the names of files and directories. For example, the following creates two directories called cistern and cavern , and a file called water.txt . Then, the third command moves the file into the cistern directory using the mv command, by specifying just enough of the cistern directory name followed by the Tab key:

$ mkdir cistern cavern $ touch water.txt $ mv water.txt ci<Tab>stern

The Tab keystroke causes Bash to look for a unique match and, in this case, it finds one ”the cistern directory ”and thus completes the directory name.

The same rule applies to files, too. The first command here changes the working directory to cistern , and the second removes the file water.txt (which you specify simply by typing w and letting Tab and the shell do the rest):

$ cd ci<Tab>stern $ rm w<Tab>ater.txt

Категории