Before you begin writing shell scripts, you should familiarize yourself a bit with the shell environment.
There are many ways to get shell access, depending on the operating system you are running.
There are four ways to get a shell prompt in Mac OS X:
Run Terminal. This is, by far, the easiest way to get a shell prompt. It has the advantage of providing access to other GUI applications at the same time. This is the recommended way to get shell access. You can find Terminal in the Utilities folder inside your Applications folder.
Connect via SSH (secure shell). First, enable “Remote Login” in the Sharing preferences pane, then use the SSH client of your choice to log in. For example, you might use the ssh
command in Terminal to run scripts on a remote computer. For more information, see the documentation for ssh
.
Use the console. In the Accounts preference pane, set the “Display login window as” setting to “Name and Password”. Then, at the login window, Type >console
as the username. (Leave the password field blank.) You will then see a text-based login prompt. Log in with your “short name” and password.
Log out (type exit
or logout
and press return) to get back to GUI-land (or just enter a few wrong passwords).
Boot single user. This environment is not generally recommended for scripting. It takes considerable effort to enable networking, external disks, and other functionality, and the root volume is mounted read-only by default. As a result, this mode is mainly useful for disaster recovery.
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.
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.
Read “Command Line Primer” to get a good overview of how to get things done in a command line environment.
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.
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" |
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.
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.
Creating a text file in TextEdit is fairly straightforward.
Create a new file by choosing File > New (from the File menu).
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.
Type or paste in the script as directed in the text.
Choose File > Save As.
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.
Name the file as directed in the text and save it.
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:
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.)
Edit the file. Use arrow keys to navigate.
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.
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