LPIC-1 Linux Professional Institute Certification Study Guide. Richard BlumЧитать онлайн книгу.
kernel being used (
2.6.32
) as well as the system's hostname (server01.class.com
). The uname
command is an external command. The shell also provides internal commands. It's important to know the difference between the two command types, as explained in the next section.
Using Internal and External Commands
Internal commands are, as you might expect, built into the shell program. Thus they are also called built-in commands. Most shells offer a similar set of internal commands, but shell-to-shell differences do exist. Internal commands that you're likely to use enable you to perform some common tasks:
Change the Working Directory
Whenever you're running a shell, you're working in a specific directory. The cd
command changes the current working directory. For instance, typing cd /home/sally changes the current working directory to the /home/sally
directory.
You can use shortcut characters with the cd
command as well. The tilde (∼
) character is a useful shortcut; it stands for your home directory. Thus typing cd ∼ will have the same effect as typing cd /home/sally if your home directory is /home/sally
.
Display the Working Directory
The pwd
command displays (“prints” to the screen) the current working directory. This command is helpful, especially after you have changed your working directory, to ensure you ended up in the right place.
Display a Line of Text
The echo
command displays the text you enter. For instance, typing echo Hello causes the system to display the string Hello
. This may seem pointless, but it's useful in scripts (described in Chapter 9, “Writing Scripts, Configuring Email, and Using Databases”), and it can also be a good way to review the contents of environment variables (described later in this chapter, in the section “Using Environment Variables”).
Time an Operation
The time
command times how long subsequent commands take to execute. For instance, typing time pwd tells you how long the system took to execute the pwd
command. The time is displayed after the full command terminates. Three times are displayed: total execution time (aka real time), user CPU time, and system CPU time. The final two values tell you about CPU time consumed, which is likely to be much less than the total execution time.
Set Options
In its most basic form, the set
command displays a wide variety of options relating to bash
shell operation. These options are formatted much like environment variables, but they aren't the same things. You can pass various options to set
to have it affect a wide range of shell operations.
Terminate the Shell
The exit
and logout
commands both terminate the shell. The exit
command terminates any shell, but the logout
command terminates only login shells. Login shells are shell programs that are launched automatically when you initiate a text-mode login as opposed to those that run in xterm
windows or other terminal emulators.
The preceding list isn't complete. Later sections of this chapter and later chapters describe some additional internal commands. Consult your shell's documentation for a complete list of its internal commands.
You can quickly determine if a command is a built-in command by using the type
command. Just enter the command type
before the name of the command you wish to check:
Some of these internal commands are duplicated by external commands that do the same thing. But those external commands aren't always installed on all systems. You can see if there are internal commands with installed duplicate external commands by using the -a
option on the type
command:
You can see that on this system, there is no external cd
command installed. However, it does have an external pwd
command installed.
Keep in mind that even when external commands are installed, the internal command takes precedence. To access the external command, you must provide the complete external command path, as in typing /usr/bin/time rather than time.
Confusion over Internal and External Commands
When duplicate internal and external commands exist, they sometimes produce subtly different results or accept different options. These differences may occasionally cause problems if you are unaware of them. For example, the time
built-in command returns slightly different results than the /usr/bin/time
external command:
As you can see, bash
's internal time
shows the time to execute the pwd
command in a very nice format, while the external time command /usr/bin/time
is not only a little sloppy in appearance, it also provides additional details. Be mindful of the potential behavior differences between internal and external commands.
When you type a command that's not recognized by the shell as one of its internal commands, the shell checks its path to find a program by that name to execute it. The path is a list of directories in which commands can be found. It's defined by the $PATH
environment variable, as described shortly in “Using Environment Variables.” A typical user account has about half a dozen or so directories in its path. You can add and remove directories to the shell's path by changing the $PATH
environment variable in a shell configuration file, as described in “Exploring Shell Configuration” later in this chapter.
You can run programs that aren't on the path by providing a complete path name on the command line. For instance, typing ./myprog runs the myprog
program in the current directory. Typing /home/arthur/thisprog runs the thisprog
program in the /home/arthur
directory.
The root
account should normally have a shorter path than ordinary user accounts. Typically, you'll omit directories that store GUI and other user-oriented programs from root
's path in order to discourage use of the root
account for routine operations. This minimizes the risk of security breaches related to buggy or compromised binaries being run by root
. Most important, root
's path should never include the current directory (./). Placing this directory in root
's path makes it possible for a local troublemaker to trick root
into running replacements for common programs. Omitting the current directory from ordinary user paths is also generally a good idea. If this directory must be part of the ordinary user path, it should appear at the end of the path so that the standard programs take precedence over any replacement programs in the current directory.
Whether you need to enter the path or not for a command, the program file must be marked as executable. This is done via the execute bit that's stored with the file. Standard programs are marked as executable when they're installed, but if you need to adjust a program's executable status, you can do so with the chmod
command, as described in Chapter 4, “Managing Files.”
Performing Some Shell Command Tricks
Many users find typing commands to be tedious and error-prone. This is particularly true of slow or sloppy typists. For this reason, Linux shells include various tools that can help speed up operations. The first of these is command completion: Type part of a command or a filename (as an option to the