6.4. Command ShortcutsBash provides a few ways to shorten commands and arguments you type at the keyboard. 6.4.1. AliasesBash allows you to define your own commands with the alias built-in command (Figure 6-14).
Here is an example of defining and using Bash command aliases: $ alias dir="ls -aF" $ dir ./ main2.c p.reverse.c reverse.h ../ main2.o palindrome.c reverse.old $ dir *.c main2.c p.reverse.c palindrome.c $ To cause an alias to no longer have a special definition, use the unalias built-in (Figure 6-15).
You might wish to undefine an alias when you want the normal behavior of a command that you normally alias to a different behavior (e.g., in our above example, if you no longer want dir to use the ls command because there is another dir command on the system). 6.4.2. Command HistoryBash maintains a historical record of the commands you type. With the commands maintained in this history, you can selectively re-execute commands or cause them to be modified and executed with changes. 6.4.2.1. Storage of CommandsCommands you have typed to the shell are stored in a history file defined by the $HISTFILE shell variable. By default, the value specifies the file ".bash_history" in the user's home directory. This file can hold a maximum of $HISTFILESIZE entries, the default value being 500. 6.4.2.2. Reading Command HistoryTo see your shell history, use the built-in history command (Figure 6-16).
6.4.2.3. Command Re-executionBash honors the "!" metacharacter to re-execute commands from the history list in the forms listed in Figure 6-17.
6.4.2.4. History substitutionSometimes you want to do more than simply re-execute a command that you have previously used. You may want to modify the command slightly (change a filename or a single argument to a long command). Figure 6-18 describes the simplest form of history substitution.
This is useful when you make a minor mistake in a command and don't want to have to retype the entire command, such as in: $ lp financial_report_july_2003.txt lp: File not found. $ ^2003^2004^ lp financial_report_july_2004.txt request id is lwcs-37 (1 file) $ _ Or, perhaps you wish to substitute something in an earlier command (because you have issued other commands to find out what was wrong, so now the command you want to repeat isn't the most recent command in this history list); Figure 6-19 shows how it's done.
For example: $ lp financial_report_july_2003.txt lp: file not found. $ ls financial_report_july_2004.txt financial_report_may_2004.txt financial_report_june_2004.txt $ !lp:s/2003/2004/ request id is lwcs-37 (1 file) $ _ 6.4.2.5. Command EditingBash provides a fairly sophisticated command-editing capability. Both emacs and vi styles of editing are supported; emacs is the default. Since, in emacs, you are always in text input mode, you can type emacs movement characters at any time while typing a command. So, for example, if you've left out a word, you can back up with Control-B and insert it. To access your history list of previous commands, you can use Control-P to move "up" as if your history list were a file. On most keyboards, the directional arrow keys can also be used for cursor movement in all four directions. Most other emacs movement commands are supported; see the section "Editing a File: Emacs" in Chapter 3, "GNU Utilities for Nonprogrammers," for more information. Bash also allows vi users the same luxury, but this must be set using the set built-in, as shown in Figure 6-20.
Because vi has two modes, "command mode" and "text input mode," while typing a normal command, Bash treats you as if you are in text input mode. Therefore, to access the vi command-editing features, you have to hit the ESCAPE key, just as you would in vi to get back into command mode. Once you do that, you can move around just as you would in vi ("h" to back up in the command, or "k" to back up to previous commands in the history list). See the section "Editing a File: vi" in Chapter 3, "GNU Utilities for Nonprogrammers," for more information. 6.4.3. Auto-CompletionBash can complete a filename, command name, username or shell variable name that you've begun typing if you've typed enough to uniquely identify it. To have Bash attempt to complete the current argument of your command, type the TAB character. If matching filenames are available but the text you've typed does not identify one completely, text that the possible names have in common will be filled in to the point where they no longer have characters in common. This gives you the option of having the shell fill in long filenames where only a few characters at the end are different (like a sequence number or a date). Then you can type only the part that is unique to the file you wish to access. Typing a second TAB character will show all possible matches. |