We have the user-friendly AppleScript and now Automator. But long before either of these existed, there were
—series of Unix commands saved as mini-programs. By learning how to write these you gain expertise that’s directly applicable to Linux and other Unix systems. Not only will you be able to use your knowledge in non-Mac environments, but you’ll have some serious Geek cred. So if you’re tired of typing command after command in Terminal, to it might be time to give shell scripts a try.
Shell scripts let you link commands together.
(|) are the key—these connect the output of one command to the input of the next. For example, the
command lists the files in your current folder (
in Unix-speak) and the
command counts number of lines. Graft these two commands together to find out how many files are in the directory. It’s as simple as opening Terminal (/Applications/Utilities) and typing
ls | wc -l
Because of the pipe, the output from the file list command (
) is received as input to the word count (
) command, which then tallies up the total number of files. Drop those two commands into a text file, give it a name with the extension
(for example, file-count.sh) and you’ve written your first simple shell script.
Shell scripts can be significantly more sophisticated, complete with loops, conditional statements, and other programming do-dads. Here I’ll give you a peek at shell scripts’ potential by looking at a cool script that plugs into Tiger’s power. Don’t be intimidated—you don’t have to type in shell scripts to use them!
Download this one
and save it to your home folder. To run a shell script, type
from within Terminal.
Capture a series of screenshots
Press Command-Shift-3 and you capture an image of your current screen to a file on the desktop. But what if you want to capture a sequence of screenshots, or want to have a handy recording device to ensure no one’s touching your computer while you’re at lunch? Tiger offers a free and straightforward solution—the command
. (For detailed information about
’s abilities, type
To capture a single screen shot, type
. The command will create a PNG file with the current screen contents and put it in the current directory, probably your home folder. (If you can’t open this image, it may be missing an extension. Add
to the end of its name.) To capture a series of screenshots, use the shell script I call screen-record.sh. It looks like this:
while [ true ] ; do
sleep 60 # 60 seconds
count=$(( $count + 1 ))
Basically, the script saves whatever base filename you specify when you start the program, then takes a screen capture, sleeps for 60 seconds, adds one to the “count” variable, and then does it all again. This script will run as long as you let it: press Ctrl-C in Terminal to make it stop.
You must specify a
when you start the script. For example, type something like
sh screen-record.sh mydesktop
. Each screenshot will be named
plus count plus the filename suffix (for example,
The count is a separate variable that increases as the script runs. I’ve written the script to have a 60 second delay between shots, but that could easily be changed to 300 for an every-five-minute capture, or even 1800 if you only want a screen capture every half hour. When you download the final script, you’ll see I added a few more bells and whistles—mostly error-checking tests.
is a 25 year Unix veteran and author of
Wicked Cool Shell Scripts
(No Starch Press, 2004) and
Learning Unix for Mac OS X Tiger
, (O’Reilly, 2005).
EDITOR’S NOTE: Intrigued by shell scripting? Download a
sample PDF chapter
from Michael Trent and Drew McCormack’s
Beginning Mac OS X Programming
to get started. It is reprinted from Beginning Mac OS X Programming by Michael Trent and Drew McCormack with the permission of Wiley Publishing, copyright 2005. For more information on this or other books visit