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 command), and then press the Tab key. The shell tries to fill in the rest of the command or the filename. If just one command or filename matches the characters you've typed so far, the shell fills the rest of the command (or filename) for you and adds a space after it.
If the characters you've typed don't uniquely identify a command (or filename), the shell fills in what it can and then stops. Depending on the shell and its configuration, it may beep. If you press the Tab key again, the system responds by displaying the possible completions. You can then type another character or two and, if you haven't completed the command (or filename), press the Tab key again to have the process repeat.
The