In a graphical interface, programs are controlled primarily by selecting and clicking menus and windows with buttons and text fields. As such, you can usually discover what a program is capable of doing just by examining the onscreen options. A command-line interface executes individual programs, called commands or tools, that you enter. A command consists of the name of the command followed by any options (often called switches) that you choose to provide, and any arguments for the command. Note that command names are case-sensitive. To execute a typed command, press the Return key. The command prompt is the starting point for entering commands in a command-line interface. It can be daunting to stare at the command prompt if you're used to a graphical interface because there are no helpful onscreen hints suggesting what to do. The command prompt indicates where you arethe name to the left of the colon (:) is the name of the computer. (If localhost is shown instead of the name of the computer, it is because a reverse DNS lookup could not be done on the computer's IP address.) The information to the right of the colon shows the working folder you are in. The tilde character (~) is an abbreviation representing your home folder. Immediately following the working folder are the name of the currently logged-in user and a separator character (%, $, or #). If the separator character # is shown, it indicates that the current user is running commands as the System Administrator. In this screen shot, the user named owen is logged in on a computer named iMac and is working in his home folder. NOTE The prompt is specific to each shell and is also user-configurable. What we show here is the default prompt you get with bash (Bourne Again Shell), the default shell. NOTE Traditionally, UNIX users and UNIX commands use the word "directory" to describe the file system structure that can contain other file system items. This book follows the Mac OS X convention of using the word "folders" to describe these file system structures. You can access the command line in Mac OS X in the following six ways:
Entering CommandsEach BSD command is entered on a single line after the shell prompt and in the following format: command option(s) (arguments) For instance, the ls command lists the contents of the named folders. Adding an option to this command gives you a little more information in the listing. Entering ls -lA ~/Documents lists the contents of your Documents folder along with their permissions for files and subfolders. Regardless of where you are in the file system, ~ refers to your home folder. Many shell commands operate on or produce textual information. Shell commands that require input typically read their input from the standard input device (the keyboard), and commands that generate output typically write to the standard output device (the monitor). Input/output redirection allows you to change the input device, the output device, or both for a given command. For example, you might want to redirect the output of an ls command to a file for inclusion in an email message. Such a command might look like this: ls -lA ~/Documents > ~/Desktop/lstext.txt The greater-than sign is used after the ls command (with its options and argument) to redirect output to a text file identified by path and filename. In this case, the list of items in the Documents folder would be saved to a text file named lstext in the current user's Desktop folder. Accessing Online HelpAll UNIX-based systems provide online help using the man (for manual) command. This command formats and displays pages describing the command, configuration file, or other item. The man pages for a command contain:
The man command, followed by a command name, displays the manual pages for that command. For instance, on the command line, enter man ls This command displays information about the ls command, including its many parameters. When you view a manual page, you are actually using a utility called more, so you can use commands to navigate the man page contents. If you press Return, you will move ahead one line. If you press the spacebar, you will move ahead one page. The up arrow and down arrow keys move up and down one line, respectively. If you are looking for a specific word on the man page, you can enter /word and press Return, and your window will jump to the next instance of that word. Press Q to exit the man page and return to the command line. You can also search the man pages for specific words using: apropos keyword where keyword is the specific word you're searching for. This command displays a list of commands whose man pages include this keyword. Using apropos is a good way to discover new commands. NOTE A newly installed system may not have had time to create the database used by the apropos command. You can force the update of the database by entering the following command: sudo /etc/weekly The man pages are organized in numbered sections. If there is more information to display than fits on one screen, press the spacebar to page down. Sometimes you have to specify the section number to find the page you want. For example, open is the name of a command, but it is also the name of a Perl language construct, and a UNIX system call. To see the man page for the Perl language construct, you need to use man 3 open To find out what a section contains, enter man section intro For instance:
There are some things to keep in mind when you use man pages as references. Not all commands have man pages, and sometimes the man pages have errors. Some man pages automatically redirect to other man pages with similar functionality. Also, because some man pages are derived from open-source documentation, they may be inaccurate, incomplete, or out-of-date. Viewing man Pages Using TerminalYou can use a command-line interface to view the man pages for UNIX commands.
Using Edit KeysText entered at the command line must be exact, and some file systems require you to enter case-sensitive filenames. Fortunately, most shells, including those included by default with Terminal, provide several keyboard shortcuts that make entering and editing commands easier. Tab CompletionTab completion allows you to type just the first unique letters of a command or path. When you press the Tab key, the shell will complete the string. When you type a partial command and press the Tab key, but there are other commands or paths that start with the same letters, the shell will respond with a beep and wait for more input. For example, suppose you want to list the contents of your Documents folder. You begin by typing ls ~/D At that point, you press the Tab key and the shell beeps because there are two folders that start with D: Desktop and Documents. If you continue by typing o and then press the Tab key again, the shell completes the folder name to ls ~/Documents/ because there is only one folder in your home folder that begins with "Do." Using the Finder for PathsA quick way to enter a very long path is to drag the folder from the Finder into the Terminal window. The path appears after the cursor on the command line, with the correct path to that folder. Shells interpret spaces in filenames as command, switch, or argument separators. To get around this, you can either put the entire path in single or double quotes, or precede each individual space with a backslash (\). When a character is preceded by a backslash, it is called "escaping the character." This prevents the shell from doing anything special with the character. When a shell performs filename completion for you, it will escape spaces in filenames. Reusing Prior CommandsThe shell maintains a list of previously entered commands. The up arrow and down arrow traverse the history list of commands. To execute a command again, press Return when it is visible at the command prompt. Editing CommandsShells often provide a number of ways to speed the editing of commands. Use Control-A to move quickly to the beginning of a line. Control-E moves the cursor to the end of the line. In addition to using the cursor keys to move the cursor forward and backward on the command line, Control-F moves forward one character and Control-B moves backward one character. To move forward and backward one word at a time, use Esc-F and Esc-B, respectively. The cursor does not need to be at the end of the line when you press Return. The shell interprets all of the text entered on the current line regardless of the cursor's location when you press Return. Miscellaneous ShortcutsUse Control-L or the clear command to clear the screen. Control-C terminates many commands in progress and will cause the shell to ignore any text currently being entered and return you to the command prompt. The following table summarizes many of the command-line entry shortcuts and actions:
|