Working with the Shell

The Mac OS X system displays a graphical interface by default. You need a shell prompt to enter commands from the CLI. To display a shell prompt, you can either run a program such as Terminal or you can connect to the system over a network.

Running the Mac OS X Command Line Interface

By default Mac OS X displays a graphical user interface. The UNIX operating system upon which OS X is based presents a textual, or command line, interface.

When you log in to a textual environment, such as from a terminal emulator or from the text console (page 35), the shell displays a prompt immediately. You are using the CLI. The easiest way to display a shell prompt from within a graphical environment is to use the Terminal utility (Figure 2-4), which is found in the Utilities folder in Applications.

Figure 2-4. A Terminal window

Tip: The command line interface

Although this section describes Terminal, it applies equally to the command line interface that OS X displays from a remote login or from the text console.

Running Commands from the Terminal Emulator/Shell

The Terminal utility (Figure 2-4) logs you in and runs a shell, which displays login messages and a prompt and waits for you to enter commands. This program is referred to as a terminal emulator, because it emulates the behavior of a serial terminal. By default Terminal emulates the behavior of a DEC vt100 terminal.

When you are typing in a terminal emulator window or using any CLI, several characters, including *, $, [, and ], have special meanings. Avoid typing these characters until you have read "Special Characters" on page 40.

The shell

When you are using a CLI, you are communicating with the command interpreter known as the shell. The shell plays an important part in much of your communication with Mac OS X. When you enter a command at the keyboard in response to the shell prompt on the screen, the shell interprets the command and initiates the appropriate action. This action may be executing your program, calling a compiler or a Mac OS X utility or another standard program, or displaying an error message telling you that you entered a command incorrectly. When you are working on a GUI, you bypass the shell and execute a program by clicking an icon or name. Refer to Chapter 5 (page 113) for more information on the shell.

Correcting Mistakes

This section explains how to correct typos and other errors you may make while you are logged in on a character-based display. Because the shell and most other utilities do not interpret the command line or other text until after you press RETURN, you can correct typing mistakes before you press RETURN.

You can correct typing mistakes in several ways: erase one character at a time, back up a word at a time, or back up to the beginning of the command line in one step. After you press RETURN, it is too late to correct a mistake; you can either wait for the command to run to completion or abort execution of the program.

Erasing a Character

While entering characters from the keyboard, you can back up and erase a mistake by pressing the erase key once for each character you want to delete. The erase key backs over as many characters as you wish. It does not, in general, back up past the beginning of the line.

Tip: CONTROL-Z suspends a program

Although not a way of correcting a mistake, you may press the suspend key (typically CONTROL-Z) by mistake and wonder what happened (you will see a message containing the word Stopped). You have just used job control (page 272) to stop your job. Give the command fg to continue your job in the foreground, and you should return to where you were before you pressed the suspend key. For more information refer to "bg: Sends a Job to the Background" on page 274.

The default erase key is DELETE. If this key does not work, try BACKSPACE or CONTROL-H. If these keys do not work, give the following stty[2] command to set the erase and line kill (see "Deleting a Line" below) keys to their defaults

[2] The command stty is an abbreviation for set teletypewriter, the first terminal that UNIX was run on. Today stty is commonly thought of as set terminal.

$ stty ek 

Deleting a Word

You can delete a word you entered by pressing CONTROL-W. A word is any sequence of characters that does not contain a SPACE or TAB. When you press CONTROL-W, the cursor moves left to the beginning of the current word (as you are entering a word) or the previous word (when you have just entered a SPACE or TAB), removing the word.

Deleting a Line

Any time before you press RETURN, you can delete a line you are entering by pressing the line kill key, or kill key. When you press this key, the cursor moves to the left, erasing characters as it goes, back to the beginning of the line. The default line kill key is CONTROL-U. If this key does not work, try CONTROL-X. If these keys do not work, give the following command to set the erase and line kill keys to their defaults:

$ stty ek 

Aborting Execution

Sometimes you may want to terminate a running program. A program may be performing a lengthy task such as displaying the contents of a file that is several hundred pages or copying a file that is not the one you meant to copy.

To terminate a program from a character-based display, press the interrupt key (CONTROL-C or sometimes DELETE or DEL). When you press this key, the operating system sends a terminal interrupt signal to the program you are running and to the shell. Exactly what effect this signal has depends on the program. Some programs stop execution immediately, whereas others ignore the signal. Some programs take other actions. When it receives a terminal interrupt signal, the shell displays a prompt and waits for another command.

If these methods do not terminate the program, try stopping the program with the suspend key (typically CONTROL-Z), giving the jobs command to verify the job number of the program, and using kill to abort the program. The job number is the number within the brackets at the left end of the line that jobs displays ([1]). The kill command uses TERM to send a termination signal[3] to the job specified by the job number, which is preceded by a percent sign (%1):

[3] When the terminal interrupt signal does not work, use the kill (KILL) signal. A running program cannot ignore kill; it is sure to abort the program (page 432).

$ bigjob ^Z [1]+  Stopped                 bigjob $ jobs [1]+  Stopped                 bigjob $ kill -TERM %1 [1]+  Stopped                 bigjob $ RETURN [1]+  Terminated              bigjob 

The kill command returns a prompt; press RETURN again to see the confirmation message. For more information on job control refer to "Running a Program in the Background" on page 131. The kill command is very similar to the Force Quit option found on the Apple menu.

Repeating/Editing Command Lines

To repeat a previous command, press the UP ARROW key. Each time you press it, you see an earlier command line. To reexecute the displayed command line, press RETURN. Press DOWN ARROW to browse through the command lines in the other direction.

The RIGHT and LEFT ARROW keys move the cursor back and forth along the displayed command line. At any point along the command line, you can add characters by typing them. Use the erase key to remove characters from the command line. For more complex techniques in command line editing, see page 296.

A Practical Guide to UNIX[r] for Mac OS[r] X Users
A Practical Guide to UNIX for Mac OS X Users
ISBN: 0131863339
EAN: 2147483647
Year: 2005
Pages: 234

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: