Before you start working in any shell environment, there are some basic features of shell programming that you should understand. Some of these features are specific to Mac OS X, but many are common to all platforms that support shell programming.
Getting Information
Specifying Files and Directories
Accessing Files on Volumes
Flow Control
At the command-line level, most documentation comes in the form of man pages. These are formatted pages that provide reference information for many shell commands, programs, and high-level concepts. To access one of these pages, you type the man command followed by the name of the thing you want to look up. For example, to look up information about the bash shell, you would type man bash. The man pages are also included in the ADC Reference Library. For more information, see Mac OS X Man Pages.
Note: Not all commands and programs have man pages. For a list of available man pages, look in the /usr/share/man directory.
Most shells have a command or man page that displays the list of built-in commands. Table A-1 lists the available shells in Mac OS X along with the ways you can access the list of built-in commands for the shell.
Shell | Command |
|---|---|
|
|
|
|
|
|
|
|
|
Most commands in the shell operate on files and directories, the locations of which are identified by paths. The directory names that comprise a path are separated by forward-slash characters. For example, the path to the Terminal program is /Applications/Utilities/Terminal.app.
Table A-2 lists some of the standard shortcuts used to represent specific directories in the system. Because they are based on context, these shortcuts eliminate the need to type full paths in many situations.
File and directory names traditionally include only letters, numbers, a period (.), or the underscore character (_). Most other characters, including space characters, should be avoided. Although some Mac OS X file systems permit the use of these other characters, including spaces, you may have to add single or double quotation marks around any pathnames that contain them. For individual characters, you can also “escape” the character, that is, put a backslash character (\) immediately before the character in your string. For example, the path name My Disk would become either "My Disk" or My\ Disk.
On a typical UNIX system, the storage provided by local disk drives is coalesced into a single monolithic file system with a single root directory. This differs from the way the Finder presents local disk drives, which is as one or more volumes, with each volume acting as the root of its own directory hierarchy. To satisfy both worlds, Mac OS X includes a hidden directory Volumes at the root of the local file system. This directory contains all of the volumes attached to the local computer. To access the contents of other local volumes, you should always add the volume path at the beginning of the remaining directory information. For example, to access the Applications directory on a volume named MacOSX, you would use the path /Volumes/MacOSX/Applications
Note: To access files on the boot volume, you are not required to add volume information, since the root directory of the boot volume is /. Including the information still works, though, and is consistent with how you access other volumes. You must include the volume path information for all other volumes.
Many programs are capable of receiving text input from the user and printing text out to the console. They do so using the standard pipes (listed in Table A-3), which are created by the shell and passed to the program automatically.
Pipe | Description |
|---|---|
The standard input pipe is the means through which data enters a program. By default, this is data typed in by the user from the command-line interface. You can also redirect the output from files or other commands to | |
The standard output pipe is where the program output is sent. By default, program output is sent back to the command line. You can also redirect the output from the program to other commands and programs. | |
The standard error pipe is where error messages are sent. By default, errors are displayed on the command line like standard output. |
From the command line you may redirect input and output from a program to a file or another program. You use the greater-than (>) character to redirect command output to a file and the less-than (<) character to use a file as input to the program. Redirecting file output lets you capture the results of running the command in the file system and store it for later use. Similarly, providing an input file lets you provide a program with preset input data, instead of requiring the user to type in that data.
In addition to file redirection, you can also redirect the output of one program to the input of another using the vertical bar (|) character. You can combine programs in this manner to implement more sophisticated versions of the same programs. For example, the command man bash | grep "builtin commands" redirects the formatted contents of the specified man page to the grep program, which searches those contents for any lines containing the word “commands”. The result is a text listing of only those lines with the specified text, instead of the entire man page.
For more information about flow control, see the man page for the shell you are using.
To terminate the current running program from the command line, type Control-C. This keyboard shortcut sends an abort signal to the current command. In most cases this causes the command to terminate, although commands may install signal handlers to trap this command and respond differently.
Last updated: 2007-10-31