Mac OS X Reference Library Apple Developer
Search

Before You Begin

Before you begin writing shell scripts, you should familiarize yourself a bit with the shell environment.

Obtaining a Shell Prompt

There are many ways to get shell access, depending on the operating system you are running.

In Mac OS X

There are four ways to get a shell prompt in Mac OS X:

In Other UNIX Variants or Linux Variants

In most other UNIX or Linux variants, you can gain access to a shell by running XTerm, GTerm, KTerm, Terminal, or some other similarly named application. Alternatively, if you log into such a machine remotely using ssh, you should get a shell prompt as soon as you log in.

Some UNIX or Linux variants provide a text-based login prompt. On these systems, you generally get a shell prompt as soon as you log in.

In Windows

Although Windows does not provide a shell, you can add one by installing Cygwin. Instructions for installing Cygwin are beyond the scope of this document. See http://www.cygwin.com/ for more information.

Note: The Cygwin environment is not a complete UNIX shell scripting environment. The examples in this document have not been tested in Cygwin and are not guaranteed to work correctly in the Cygwin environment.

Familiarize Yourself With the Command Line

Read “Command Line Primer” to get a good overview of how to get things done in a command line environment.

Tips for Shell Users

While this document is primarily focused on writing shell scripts, there are a few helpful tips that can be useful to shell users and programmers alike. This section includes a few of those tips.

The alias Builtin

Various Bourne shells also offer a number of other builtin commands that you may find useful, one of the more useful for command-line users being alias. This command allows you to assign a short name to replace a longer command. While the alias builtin is not frequently used in shell scripts (unless you are intentionally trying to obfuscate your code), it is very convenient when using the shell interactively. For example:

alias listsource="ls *.c *.h"

Typing the command listsource after entering this line will result in listing all of the .c and .h files in the current directory.

For more information, see the man page builtins, or for ZSH, zshbuiltins.

C Shell Note: The C shell syntax is similar, but not identical. In the C shell, the equals sign is replaced with a space. For example:

alias listsource "ls *.c *.h"

Entering Special Characters

Some shells treat tabs and other control characters in special ways. When writing a script in a text file, the reuse of these characters for shell-specific purposes is not generally an issue. However, when entering commands on the command line, it may get in the way if you need to enter any of these characters as part of a command for some reason.

To enter a tab or other control character on the command line, type control-v followed by the tab key or other control character. The control-v tells the shell to treat whatever character comes next literally without interpreting it in any way during entry.

For example, to enter the ASCII bell character (control-G), you can type the following:

echo "control-V control-G"

This will be seen on your screen as:

echo "^G"

When you press return, your computer should beep.

Creating Text Files in Your Home Directory

In various parts of this document, you need to create a text file and save it into your home directory.

In Terminal, your home directory is the directory that you are in when you first open the Terminal window.

In the rest of Mac OS X, your home directory can be found in the “PLACES” list in Finder window sidebars, Save dialog sidebars, and so on. It's the icon that looks like a house. Your home directory is also the default location if you create a new finder window by choosing File > New Finder Window in Finder.

In TextEdit

Creating a text file in TextEdit is fairly straightforward.

  1. Create a new file by choosing File > New (from the File menu).

  2. Choose Format > Make Plain Text.

    By default, TextEdit saves files in Rich Text Format (RTF). Choosing Make Plain Text from the Format menu tells it that you want to work with a plain text file instead.

  3. Type or paste in the script as directed in the text.

  4. Choose File > Save As.

  5. In the resulting Save dialog, look in the sidebar on the left. Scroll it until you see the “PLACES” section, then click the house icon beside your username.

  6. Name the file as directed in the text and save it.

In pico or nano

If you are logging into a computer remotely using SSH, you must use a text editor that can be run on the command line (unless you use X11 forwarding and an X11-based editor).

The pico and nano commands are two very easy command-line text editors. At least one of these commands is available in most UNIX or Linux-based operating systems.

To create a text file in NANO or PICO:

  1. Type nano filename or pico filename and press return. (Type the name of the file you want to create or edit instead of the word filename.)

  2. Edit the file. Use arrow keys to navigate.

  3. When you are finished editing, press Control-O. Adjust the name of the file (if desired), then press return to save the file to disk.

  4. To exit the editor, press Control-X.

For other valid commands, see the list of control characters along the bottom of the screen or press Control-G for more complete documentation.




Last updated: 2010-06-18

Did this document help you? Yes It's good, but... Not helpful...