To use commands, you type them into the shell at the prompt. The shell executes the command line and displays output (if any), and then gives you another shell prompt. When the shell prompt comes up again, even if there's no other output, your shell is ready to accept another command.
Many command lines (but not all) produce output before returning a new shell prompt. It is quite common in Unix for a command to produce no visible output if it is successful (if it fails, a command should always produce output). In Unix, silence implies success.
To run a command:
This is the ls command, which lists the names of files and directories. The output of the commanda list of the tools installed in the /Developer/Tools directoryappears, and then a new shell prompt follows ( Figure 2.4 ).
user-vc8f9gd:~ vanilla$ ls /Developer/Tools BuildStrings RezWack mdcheckschema CpMac SetFile mdimport DeRez SplitForks momc GetFileInfo UnRezWack packagemaker MergePef WSMakeStubs pbhelpindexer MvMac agvtool pbprojectdump PPCExplain cvs-unwrap uninstall-devtools.pl ResMerger cvs-wrap Rez firewire user-vc8f9gd:~ vanilla$
The command line you just used consists of two parts : the command ( ls ) and an argument ( /Developer/Tools ).
The parts of a command line are separated by spaces. Basic command lines have up to four kinds of components :
The command (required)
Options (or switches or flags ) (optional)
Operators and special characters (optional)
Figure 2.5 shows the different parts of a typical command line (you'll recognize this as the command from Figure 1.4 in Chapter 1, "What Is Unix, and Why Is It Good?" that searched for all instances of the word success in a particular directory).
Each command may have multiple options and multiple arguments.
When you enter a command line, the shell assumes that the first item on the line is a command.
There are two types of commands: those that are built into the shell you are using and those that are separate files somewhere on your disk. For example, if you run the cd command from the bash shell, you will execute the separate program /usr/bin/cd ; the tcsh shell has a built-in cd command.
The overwhelming majority of Unix commands are separate programsthat is, Unix commands are usually individual files that are actually small (or not-so-small) programs that perform a specific function, such as listing the contents of a directory (that would be the /bin/ls command).
Unix Commands vs. Mac Applications
Traditional Macintosh applications tend to have a great many features that allow you to accomplish complete projects all from within one application. For example, you can create and manipulate complex documents in a page-layout program. Unix takes a different approach.
In Unix, commands are often focused on specific steps you use in a variety of different tasks. For example, where the Mac has a single application (the Finder) for performing many tasks involving files, Unix uses a collection of separate "applications": the ls command lists the contents of a directory, the cd command switches from one directory to another, the cp command copies files, the mv command renames files, and so on.
This difference in approach shows a key difference in philosophy between the traditional Mac and Unix ways of thinking. In Unix, you are expected to combine commands in various ways to accomplish your work; in traditional Mac applications, the program's author is expected to have anticipated every kind of task you might want to accomplish and provided a way of doing that.
Unix provides a collection of smaller, "sharper" tools and expects you to decide how to put them together to accomplish your goals.
When the shell sees a command, it evaluates whether it is a built-in commandthat is, one that is part of the shell itself. If the command is not built in, then the shell assumes the command is an actual file on the disk and looks for it.
If the command does not contain any / (slash) characters, then the shell searches in a list of places known as your PATH for a file whose name matches the command name (see the description of the PATH environment variable in Chapter 7). If the command contains any / characters, then the shell assumes you are telling it not to search your PATH but instead to interpret the command as a relative or absolute path to the command file. Relative and absolute paths are two ways of specifying Unix filenames on the command line, and we explain relative and absolute paths in Chapter 5.
Options (also called switches or flags ) modify the way a command behaves. Most commands have at least a few options available, and many commands have a large number of options. As we noted when we talked about Unix's flexibility in Chapter 1, options frequently can be combined.
See Chapter 3 to learn how to ascertain the available options for each command.
To use one option with a command:
user-vc8f9gd:~ vanilla$ ls -s /Developer/Tools total 2144 48 BuildStrings 448 WSMakeStubs 56 CpMac 32 agvtool 224 DeRez 8 cvs-unwrap 40 GetFileInfo 8 cvs-wrap 256 MergePef 0 firewire 56 MvMac 64 mdcheckschema 168 PPCExplain 8 mdimport 48 ResMerge 72 momc 264 Rez 8 packagemaker 48 RezWack 40 pbhelpindexer 48 SetFile 56 pbprojectdump 48 SplitForks 48 uninstall-devtools.pl 48 UnRezWack user-vc8f9gd:~ vanilla$
Here's a case where we want to combine two options in a command. The -s option gave us file sizes, using a unit of measurement (blocks) that might not always have the same value (blocks are usually 512 bytes but might vary, depending on a setting called BLOCKSIZE ). If we add the -k option, the sizes are shown in kilobytes, regardless of how BLOCKSIZE is set. ( BLOCKSIZE is an environment variable ; see Chapter 7.)
To use multiple options with a command:
user-vc8f9gd:~ vanilla$ ls -s -k /Developer/Tools total 1072 24 BuildStrings 224 WSMakeStubs 28 CpMac 16 agvtool 112 DeRez 4 cvs-unwrap 20 GetFileInfo 4 cvs-wrap 128 MergePef 0 firewire 28 MvMac 32 mdcheckschema 84 PPCExplain 4 mdimport 24 ResMerger 36 momc 132 Rez 4 packagemaker 24 RezWack 20 pbhelpindexer 24 SetFile 28 pbprojectdump 24 SplitForks 24 uninstall-devtools.pl 24 UnRezWack user-vc8f9gd:~ vanilla$ ls -sk /Developer/Tools total 1072 24 BuildStrings 224 WSMakeStubs 28 CpMac 16 agvtool 112 DeRez 4 cvs-unwrap 20 GetFileInfo 4 cvs-wrap 128 MergePef 0 firewire 28 MvMac 32 mdcheckschema 84 PPCExplain 4 mdimport 24 ResMerger 36 momc 132 Rez 4 packagemaker 24 RezWack 20 pbhelpindexer 24 SetFile 28 pbprojectdump 24 SplitForks 24 uninstall-devtools.pl 24 UnRezWack
Most commands accept one or more arguments . An argument is a piece of information the command acts upon, such as the name of a file to display. It's similar to the object of a sentence .
You used a command with a single argument in the tasks above. The single argument was /Developer/Tools , the folder whose contents you wanted to list. A command line can contain multiple arguments.
To use multiple arguments with a command:
user-vc8f9gd:~ vanilla$ ls /Developer /Developer/Tools /Developer: ADC Reference Library Extras Applications Headers Palettes Documentation Java Private Examples Makefiles Tools /Developer/Tools: BuildStrings RezWack mdcheckschema CpMac SetFile mdimport DeRez SplitForks momc GetFileInfo UnRezWack packagemaker MergePef WSMakeStubs pbhelpindexer MvMac agvtool pbprojectdump PPCExplain cvs-unwrap uninstall-devtools.pl ResMerger cvs-wrap Rez firewire user-vc8f9gd:~ vanilla$
You can combine multiple options with multiple argumentsfor example,
ls -sk /Developer /Developer/Tools
Remember that the shell expects the parts of a command line to be separated by spaces. If an argument has spaces in it, then you need to protect the embedded space(s) from being interpreted as separators. See "About Spaces in the Command Line," later in the chapter.
A number of special characters often appear in command lines, most frequently the > , , and & characters.
These special characters are used for a variety of powerful features that manipulate the output of commands. The most common of these operators make it easy to save the output of a command to a file, feed the output of one command into another command, use the output of one command as an argument to another command, and run a command line "in the background" (that is, letting you get a shell prompt back even if the command takes an hour to run).
The use of these powerful features is covered later in this chapter (see "Creating Pipelines of Commands").
Table 2.1 summarizes the most frequently used command-line operators and special characters, with examples of their use.
E XAMPLE AND M EANING
command > filename
Redirect output to file .
command >> filename
Redirect output, appending to file .
command < filename
command gets input from file .
cmdA cmdB (sometimes called the pipe character)
Pipe output of cmdA into cmdB .
Run command in background, returning to shell prompt at once.
cmdA `cmdB` (often called backtick characters)
Execute cmdB first, then use output as an argument to cmdA .
Some commands run for a long time, and sometimes they can get "stuck" (perhaps because a command is waiting for some other process to finish, or because of a network problem, or for any number of other reasons) and neither give output nor return you to a shell prompt. In those cases, you need a way to stop a command once you have started it. Here are two ways to stop a command.
If you are waiting for the shell prompt to appear, then you use to stop the command.
To stop a command with Control-C:
If using doesn't work, as a last resort you can close the Terminal window, overriding the warning that appears. The stuck command will be stopped . It doesn't hurt Unix for you to close the window; it's just annoying for you.
To stop a command using the kill command:
Most commands have associated documentation in the Unix help system. Unfortunately, Unix help is almost always written for the experienced programmer, not for the novice user, so we have devoted all of Chapter 3, "Getting Help and Using the Unix Manual," to clarifying it.
You can skip ahead to Chapter 3 and come back if you like, but here is the bare minimum you need to at least begin to explore the help available for commands.
To read the Unix manual for a command:
This displays the Unix manual for a command. Figure 2.9 shows the first screen of the manual for the ls command, displayed by typing man ls .
Figure 2.9. When you request help from the Unix manual by typing in a command like man ls , you get a page with an explanation of that entry (this shows only part of an output).
The man command displays the Unix manual entry for the named command, one screen at a time.
To move forward one screen, press the once.
To move backward one screen, press once.
To quit from the man command and return to a shell prompt: .
You should be back at the shell prompt.