$ type bash bash is /bin/bash
Try these few words with the type
command to see other locations of commands: which
, case
, and return
. If a command resides in several locations, you can add the -a
option to have all of the known locations of the command printed. For example, the command type -a ls
should show an aliased and filesystem location for the ls
command.
Sometimes, you run a command and receive an error message that the command was not found or that permission to run the command was denied. If the command was not found, check that you spelled the command correctly and that it is located in your PATH
variable. If permission to run the command was denied, the command may be in the PATH
variable but may not be executable. Also remember that case is important, so typing CAT or Cat will not find the cat
command.
If a command is not in your PATH
variable, you can use the locate
command to try to find it. Using locate
, you can search any part of the system that is accessible to you. (Some files are only accessible to the root user.) For example, if you wanted to find the location of the chage
command, you could enter the following:
$ locate chage /usr/bin/chage /usr/sbin/lchage /usr/share/man/fr/man1/chage.1.gz /usr/share/man/it/man1/chage.1.gz /usr/share/man/ja/man1/chage.1.gz /usr/share/man/man1/chage.1.gz /usr/share/man/man1/lchage.1.gz /usr/share/man/pl/man1/chage.1.gz /usr/share/man/ru/man1/chage.1.gz /usr/share/man/sv/man1/chage.1.gz /usr/share/man/tr/man1/chage.1.gz
Notice that locate
not only found the chage
command, it also found the lchage
command and a variety of man pages associated with chage
for different languages. The locate
command looks all over your filesystem, not just in directories that contain commands. (If locate
does not find files recently added to your system, run updatedb
as root to update the locate database.)
In the coming chapters, you learn to use additional commands. For now, I want you to become more familiar with how the shell itself works. So next I discuss features for recalling commands, completing commands, using variables, and creating aliases.
Recalling Commands Using Command History
Being able to repeat a command you ran earlier in a shell session can be convenient. Recalling a long and complex command line that you mistyped can save you some trouble. Fortunately, some shell features enable you to recall previous command lines, edit those lines, or complete a partially typed command line.
The shell history is a list of the commands that you have entered before. Using the history
command in a bash shell, you can view your previous commands. Then using various shell features, you can recall individual command lines from that list and change them however you please.
The rest of this section describes how to do command-line editing, how to complete parts of command lines, and how to recall and work with the history list.
If you type something wrong on a command line, the bash shell ensures that you don't have to delete the entire line and start over. Likewise, you can recall a previous command line and change the elements to make a new command.
By default, the bash shell uses command-line editing that is based on the emacs text editor. (Type man emacsto read about it, if you care to do so.) If you are familiar with emacs, you probably already know most of the keystrokes described here.
If you prefer the vi
command for editing shell command lines, you can easily make that happen. Add the following line to the .bashrc
file in your home directory:
set -o vi
The next time you open a shell, you can use vi
commands to edit your command lines.
To do the editing, you can use a combination of control keys, meta keys, and arrow keys. For example, Ctrl+F means to hold down the Ctrl key, and type f. Alt+F means to hold down the Alt key, and type f. (Instead of the Alt key, your keyboard may use a Meta key or the Esc key. On a Windows keyboard, you can use the Windows key.)
To try out a bit of command-line editing, enter the following:
$ ls /usr/bin | sort -f | less
This command lists the contents of the /usr/bin
directory, sorts the contents in alphabetical order (regardless of case), and pipes the output to less
. The less
command displays the first page of output, after which you can go through the rest of the output a line (press Enter) or a page (press spacebar) at a time. Simply press qwhen you are finished. Now, suppose that you want to change /usr/bin
to /bin
. You can use the following steps to change the command:
1 Press the up arrow (↑) key. This displays the most recent command from your shell history.
2 Press Ctrl+A. This moves the cursor to the beginning of the command line.
3 Press Ctrl+F or the right arrow (→) key. Repeat this command a few times to position the cursor under the first slash (/).
4 Press Ctrl+D. Type this command four times to delete /usr from the line.
5 Press Enter. This executes the command line.
As you edit a command line, at any point you can type regular characters to add those characters to the command line. The characters appear at the location of your text cursor. You can use right → and left ← arrows to move the cursor from one end to the other on the command line. You can also press the up ↑ and down ↓ arrow keys to step through previous commands in the history list to select a command line for editing. (See the section “Command-line recall” for details on how to recall commands from the history list.) You can use many keystrokes to edit your command lines. Table 3.1lists the keystrokes that you can use to move around the command line.
TABLE 3.1 Keystrokes for Navigating Command Lines
Keystroke |
Full Name |
Meaning |
Ctrl+F |
Character forward |
Go forward one character. |
Ctrl+B |
Character backward |
Go backward one character. |
Alt+F |
Word forward |
Go forward one word. |
Alt+B |
Word backward |
Go backward one word. |
Ctrl+A |
Beginning of line |
Go to the beginning of the current line. |
Ctrl+E |
End of line |
Go to the end of the line. |
Ctrl+L |
Clear screen |
Clear screen and leave line at the top of the screen. |
The keystrokes in Table 3.2can be used to edit command lines.
TABLE 3.2 Keystrokes for Editing Command Lines
Keystroke |
Full Name |
Meaning |
Ctrl+D |
Delete current |
Delete the current character. |
Backspace |
Delete previous |
Delete the previous character. |
Ctrl+T |
Transpose character |
Switch positions of current and previous characters. |
Alt+T |
Transpose words |
Switch positions of current and previous words. |
Alt+U |
Uppercase word |
Change the current word to uppercase. |
Alt+L |
Lowercase word |
Change the current word to lowercase. |
Alt+C |
Capitalize word |
Change the current word to an initial capital. |
Ctrl+V |
Insert special character |
Add a special character. For example, to add a Tab character, press Ctrl+V+Tab. |
Use the keystrokes in Table 3.3to cut and paste text on a command line.
Читать дальше