Specifications

23
Instead of entering commands and waiting for their
responses, you can compose scripts that are run without
direct interaction.
This chapter discusses some basics of shell scripting on Mac OS X, including
automation and scheduling, as well as a brief overview of what a shell script is.
It does not provide information on writing shell scripts in general.
For information about how to write shell scripts, see the Shell Scripting Primer on
the Apple Developer Connection website.
What is a Shell Script?
A shell script is a text le that contains one or more UNIX commands. You run a shell
script to perform commands you might otherwise run interactively at the command line.
Shell scripts are useful because you can combine many common tasks into one script,
saving you time and possible errors when running similar tasks over and over. They
can also be easily automated using tools such as launchd or Apple Remote Desktop.
A shell script begins with a character combination that identies it as a shell script, the
characters ‘#’ and ‘!’ (together called a “shebang”) followed by a reference to the specic
shell that the script should be run with. For example, here’s the rst line of a shell script
that would be run with sh:
#!/bin/sh
You should document your shell scripts with comments. To make a comment, start
the line with the number sign (#). Every line of a comment needs to begin with the
number sign:
#This program returns the
#contents of my Home folder
You can put blank lines in a shell script to help visually distinguish dierent sections
of the script.
3
Scripting the Command Line