< Previous PageNext Page > Hide TOC

Command Line Primer

A command-line interface is a way for you to manipulate your computer in situations where a graphical approach is not available. The Terminal application is the Mac OS X gateway to the BSD command-line interface. Each window in Terminal contains a complete execution context, called a shell, that is separate from all other execution contexts. The shell itself is an interactive programming language interpreter, with a specialized syntax for executing commands and writing structured programs, called shell scripts. A shell remains active as long as its Terminal window remains open.

Different shells feature slightly different capabilities and programming syntax. Although you can use any shell of your choice, the examples in this book assume that you are using the standard Mac OS X shell. The standard shell is bash if you are running Mac OS X v10.3 or later and tcsh if you are running an earlier version of the operating system.

The following sections provide some basic information and tips about using the command-line interface more effectively; they are not intended as an exhaustive reference for using the shell environments.

In this section:

Basic Shell Concepts
Frequently Used Commands
Environment Variables
Running Programs


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.

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.

Frequently Used Commands

Shell programming involves a mixture of built-in shell commands and standard programs that run in all shells. While most shells offer the same basic set of commands, there are often variations in the syntax and behavior of those commands. In addition to the shell commands, Mac OS X also provides a set of standard programs that run in all shells.

Table A-4 lists some of the more commonly used commands and programs. Because most of the items in this table are not built-in shell commands, you can use them from any shell. For syntax and usage information for each command, see the corresponding man page. For a more in-depth list of commands and their accompanying documentation, see Mac OS X Man Pages.

Table A-4  Frequently used commands and programs

Command

Meaning

Description

cat

Catenate

Catenates the specified list of files to stdout.

cd

Change Directory

A common shell command used to navigate the directory hierarchy.

cp

Copy

Copies files and directories (using the -r option) from one location to another.

date

Date

Displays the current date and time using the standard format. You can display this information in other formats by invoking the command with specific arguments.

echo

Echo to Output

Writes its arguments to stdout. This command is most often used in shell scripts to print status information to the user.

less

Scroll Through Text

Used to scroll through the contents of a file or the results of another shell command. This command allows forward and backward navigation through the text.

ls

List

Displays the contents of the current directory. Specify the -a argument to list all directory contents (including hidden files and directories). Use the -l argument to display detailed information for each entry.

mkdir

Make Directory

Creates a new directory.

more

Scroll Through Text

Similar to the less command but more restrictive. Allows forward scrolling through the contents of a file or the results of another shell command.

mv

Move

Moves files and directories from one place to another. You also use this command to rename files and directories.

open

Open an application or file.

You can use this command to launch applications from Terminal and optionally open files in that application.

pwd

Print Working Directory

Displays the full path of the current directory.

rm

Remove

Deletes the specified file or files. You can use pattern matching characters (such as the asterisk) to match more than one file. You can also remove directories with this command, although use of rmdir is preferred.

rmdir

Remove Directory

Deletes a directory. The directory must be empty before you delete it.

Ctrl-C

Abort

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.

Environment Variables

Some programs require the use of environment variables for their execution. Environment variables are variables inherited by all programs executed in the shell’s context. The shell itself uses environment variables to store information such as the name of the current user, the name of the host computer, and the paths to any executable programs. You can also create environment variables and use them to control the behavior of your program without modifying the program itself. For example, you might use an environment variable to tell your program to print debug information to the console.

To set the value of an environment variable, you use the appropriate shell command to associate a variable name with a value. For example, in the bash shell, to set the variable MYFUNCTION to the value MyGetData in the global shell environment you would type the following command in a Terminal window:

% export MYFUNCTION=MyGetData

When you launch an application from a shell, the application inherits much of its parent shell’s environment, including any exported environment variables. This form of inheritance can be a useful way to configure the application dynamically. For example, your application can check for the presence (or value) of an environment variable and change its behavior accordingly. Different shells support different semantics for exporting environment variables, so see the man page for your preferred shell for further information.

Although child processes of a shell inherit the environment of that shell, shells are separate execution contexts and do not share environment information with one another. Thus, variables you set in one Terminal window are not set in other Terminal windows. Once you close a Terminal window, any variables you set in that window are gone. If you want the value of a variable to persist between sessions and in all Terminal windows, you must set it in a shell startup script.

Another way to set environment variables in Mac OS X is with a special property list in your home directory. At login, the system looks for the following file:

~/.MacOSX/environment.plist

If the file is present, the system registers the environment variables in the property-list file. For more information on configuring environment variables, see Runtime Configuration Guidelines.

Running Programs

To run a program in the shell, you must type the complete pathname of the program’s executable file, followed by any arguments, and then press the Return key. If a program is located in one of the shell’s known directories, you can omit any path information and just type the program name. The list of known directories is stored in the shell’s PATH environment variable and includes the directories containing most of the command-line tools.

For example, to run the ls command in the current user’s home directory, you could simply type it at the command line and press the Return key.

host:~ steve$ ls

If you wanted to run a tool in the current user’s home directory, however, you would need to precede it with the directory specifier. For example, to run the MyCommandLineProgram tool, you would use something like the following:

host:~ steve$ ./MyCommandLineProgram

To launch an application package, you can either use the open command (open MyApp.app) or launch the application by typing the pathname of the executable file inside the package, usually something like ./MyApp.app/Contents/MacOS/MyApp.



< Previous PageNext Page > Hide TOC


© 2004, 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-10-15)


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.