1.3. Using the Terminal

 < Day Day Up > 

The first order of business when exploring a new flavor of Unix is to find the command prompt. In Mac OS X, you won't find the command prompt in the Dock or on a Finder menu. Instead, you'll need to use the Terminal application, located in /Applications/Utilities. Don't open it just yet, though. First, drag Terminal's application icon to the Dock so you'll have quick access to it when you need to access the command line. To launch the Terminal, click its icon in the Dock once, or double-click on its icon in the Finder.

The full path to the Terminal is /Applications/Utilities/Terminal.app, although the Finder hides the .app extension. Terminal.app is not a binary file. Instead, it's a Mac OS X bundle, which contains a collection of files, including the binary and support files for the Terminal's user interface.

You can Control-click (or right-click) on the Terminal in the Finder and select Show Package Contents to see what's inside. You can also use the Unix commands ls and cd to explore the directory /Applications/Utilities/Terminal.app.


After the Terminal starts, you are greeted by the banner message from /etc/motd and a bash prompt, as shown in Figure 1-1.

Figure 1-1. The Terminal window


1.3.1. Launching Terminals

One difference xterm users will notice is that there is no obvious way to launch a new Terminal window from the command line. For example, the Mac OS X Terminal has no equivalent to the following commands:

     xterm &     xterm -e -fg green -bg black -e pine -name pine -title pine & 

Instead, you create a new Terminal window by pressing -N or selecting File New Shell from the menu bar.

To cycle between open Terminal windows, you can use the same keystroke that most other Mac OS X applications use: -". You can also switch between windows by pressing -Right Arrow or -Left Arrow, using the Window menu, or by using the Terminal's Dock menu. You can also jump to a particular Terminal window with -number (see the Window menu for a list of numbers).


You can customize startup options for new Terminal windows by creating .term and .command files.

1.3.1.1. .term files

You can launch a customized Terminal window from the command line by saving some prototypical Terminal settings to a .term file and then using the open command to launch the .term file. (For more information on open, see "The open Command section, later in this chapter.) To create a .term file, open a new Terminal window, and then open the Terminal Inspector (File Show Info or -I) and set the desired attributes, such as window size, fonts, and colors. When the Terminal's attributes have been set, save the Terminal session (File Save or -S) to a .term file, such as proto.term . If you save this file to ~/Library/Application Support/Terminal, you'll be able to launch a new Terminal window with the proto.term file's special attributes from the File Library menu.

Alternatively, you can launch such a Terminal window from the command line by issuing the following command (depending on where you saved proto.term):

     open ~/Library/Application\ Support/Terminal/proto.term     open ~/Documents/proto.term 

You can also double-click on proto.term in the Finder to launch a Terminal window.


The .term file is an XML property list (plist) that you can edit with a text editor like vim (it can be invoked with vi, which is a symbolic link to vim) or with the Property List Editor application (/Developer/Applications/Utilities).[*] By default, opening the .term file creates a new Terminal window. You can configure the window so it executes a command by adding an execution string to the .term file. When you launch the Terminal, this string is echoed to standard output before it is executed. Example 1-1 shows an execution string that connects to a remote host via ssh and exits when you log out.

[*] For more information on XML, see Learning XML (O'Reilly) or XML in a Nutshell (O'Reilly).

Example 1-1. An execution string to connect to a remote host
     <key>ExecutionString</key>     <string>ssh xyzzy.oreilly.com; exit</string> 

1.3.1.2. .command files

Adding the .command extension to any executable shell script turns it into a double-clickable executable. The effect is similar to that of a .term file, except that you can't control the Terminal's characteristics in the same way. (A .command file uses the Terminal's default settings.) However, you can stuff the shell script full of osascript commands to set the Terminal characteristics after it launches. The osascript utility lets you run AppleScript from the command line.[*] Example 1-2 is a shell script that sets the size and title of the Terminal, and then launches the pico editor.

[*] More details on Scripting are covered in this book in Chapter 19. To learn more about AppleScript, see AppleScript: The Definitive Guide (O'Reilly).

Example 1-2. Launching the pico editor
     #!/bin/sh     # Script RunPico.command     osascript  <<EOF     tell app "Terminal"       set number of rows of first window to 34       set number of columns of first window to 96       set custom title of first window to "PICO Editor"     end tell     EOF     pico $@ 

If you don't want to give the shell a .command extension, you could also use the Finder's Get Info option (File Get Info or -I) to choose which application opens the executable. To do this, perform the following steps:

  1. Highlight the script's icon in the Finder.

  2. Choose Get Info from the File menu (-I).

  3. In the Get Info dialog, choose "Open with."

  4. Click the drop-down menu and choose Other.

  5. In the Choose Other Application dialog, select All Applications rather than Recommended Applications.

  6. Find and choose the Terminal (/Applications/Utilities) application.

  7. Click Add.

  8. Close the Get Info window (-W).

You can assign a custom-made icon to your shell scripts, and place them in the right section of the Dock. You can also drag the executable's icon to the lower section of the Finder's Sidebar, although this section of the Finder is intended primarily for quick access to frequently visited folders. To change an icon, use the following procedure:

  1. Copy the desired icon to the clipboard.

  2. Select your script in the Finder and open the Get Info window (-I). The file's icon appears in the upper-left corner.

  3. Click the current icon, and use the Paste option (Edit Paste or -V) to paste the new icon over it.

  4. Close the Get Info window (-W) to save the icon to the application.

To add the shell script application to the Dock, locate the application in the Finder and drag its icon to the Dock. Now you can click on the script's Dock icon to invoke the script.

1.3.2. Split Screen Terminal Feature

You can split a Terminal window (see Figure 1-2) into upper and lower sessions by clicking on the small broken rectangle located just above the Terminal's scroll bar. This feature is useful, for example, if you need to edit a file and copy and paste output from earlier in the Terminal session. The upper window contains the buffer (i.e., what you would see if you scrolled up in a non-split window), while the lower window contains your current Terminal section.

1.3.3. Contextual Menu

Users familiar with the X Window System know that right-clicking an xterm window opens a terminal-related contextual menu . Mac OS X's Terminal also has a contextual menu that can be accessed with Control-clicking (or

Figure 1-2. The Terminal's split screen


right-clicking if you have a two- or three-button mouse). The Terminal contextual menu includes the following choices: Copy, Paste, Paste Selection, Paste Escaped Text, Select All, Clear Scrollback, Send Break (equivalent to Control-C), Send Hard Reset, Send Reset, and Windows Settings. Each of these items also has a keyboard shortcut.

     < Day Day Up > 


    Mac OS X Tiger for Unix Geeks
    Mac OS X Tiger for Unix Geeks
    ISBN: 0596009127
    EAN: 2147483647
    Year: 2006
    Pages: 176

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net