Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Basic Shell Concepts

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.

In this section:

Getting Information
Specifying Files and Directories
Accessing Files on Volumes
Flow Control


Getting Information

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.

Table A-1  Getting a list of built-in commands

Shell

Command

bash

help or bash

sh

help or sh

csh

builtins or csh

tcsh

builtins or tcsh

zsh

zshbuiltins

Specifying Files and Directories

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.

Table A-2  Special path characters and their meaning

Path string

Description

.

A single period represents the current directory. This value is often used as a shortcut to eliminate the need to type in a full path. For example, the string “./Test.c” represents the Test.c file in the current directory.

..

Two periods represents the parent directory of the current directory. This string is used for navigating up one level from the current through the directory hierarchy. For example, the string “../Test” represents a sibling directory (named Test) of the current directory.

~

The tilde character represents the home directory of the currently logged-in user. In Mac OS X, this directory either resides in the local /Users directory or on a network server. For example, to specify the Documents directory of the current user, you would specify ~/Documents.

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.

Accessing Files on Volumes

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.

Flow Control

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.

Table A-3  Input and output sources for programs

Pipe

Description

stdin

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 stdin.

stdout

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.

stderr

The standard error pipe is where error messages are sent. By default, errors are displayed on the command line like standard output.

Redirecting Input and 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.

Terminating Programs

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.



< Previous PageNext Page > Hide TOC


Last updated: 2007-10-31




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice