Section 6.4. Command Shortcuts


[Page 217]

6.4. Command Shortcuts

Bash provides a few ways to shorten commands and arguments you type at the keyboard.

6.4.1. Aliases

Bash allows you to define your own commands with the alias built-in command (Figure 6-14).

Figure 6-14. Description of the alias shell command.

Shell Command: alias [-p] [word[=string]]

If you alias a new command word equal to string, then when you type the command word the string will be used in its place (and any succeeding arguments will be appended to string) and the command will be evaluated. In the usage "alias word" any alias defined for word will be printed. Its simplest usage "alias" will print all defined aliases. If the -p argument is used, the aliases are printed in a format suitable for input to the shell (so if you've manually set up aliases you like, you can write them to a file to include in your .bashrc file).


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).

Figure 6-15. Description of the unalias shell command.

Shell Command: unalias [-a] {word}+

Remove the specified alias(es). If "-a" is used, remove all aliases.


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 History

Bash 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.


[Page 218]
6.4.2.1. Storage of Commands

Commands 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 History

To see your shell history, use the built-in history command (Figure 6-16).

Figure 6-16. Description of the history shell command.

Shell Command: history [-c] [n]

Print out the shell's current command history. If a numeric value n is specified, show only the last n entries in the history list. If "-c" is used, clear the history list.


6.4.2.3. Command Re-execution

Bash honors the "!" metacharacter to re-execute commands from the history list in the forms listed in Figure 6-17.

Figure 6-17. Command re-execution metacharacters in Bash.

Form

Action

!!

Replaced with the text of the last command.

!number

Replaced with command number number in the history list.

!-number

Replaced with the text of the command number commands back from the end of the list (!-1 is equivalent to !!).

!prefix

Replaced with the text of the last command that started with prefix.

!?substring?

Replaced with the text of the last command that contained substring.


6.4.2.4. History substitution

Sometimes 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.


[Page 219]

Figure 6-18. Description of simple history substitution in Bash.

^string1^string2^

Substitute string2 for string1 in the previous command and executes it.


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.

Figure 6-19. Example of more complex history substitution in Bash.

!command:s/string1/string2/

string2 is substituted for string1 in the most recent command that begins with the text specified by command.


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 Editing

Bash 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.


[Page 220]

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.

Figure 6-20. Example of the set built-in to set the command-line edit style.

Shell command: set o vi

Tell the shell to use vi-style command editing. If you ever want to return to the default emacs-style, substitute "emacs" for "vi" in the above command.


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-Completion

Bash 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.




Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

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