05.01.2013 Views

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

332<br />

CHAPTER 19 EXTENDING THE POWER OF DARWIN<br />

NOTE The possible names a variable can have are virtually endless, but there are some rules.<br />

First, although the name can contain letters, numbers, or underscores, it’s best that they begin<br />

with a letter (and they can’t begin with a number, since that is reserved for command-line argument<br />

variables). Second, variables obviously can’t share a name with a reserved word, which is<br />

one of the many words that have special meaning to the shell. Finally, by convention, environmental<br />

variables are often written with all capital letters, and shell variables are traditionally<br />

written in all lowercase letters, but of late many people use camel notation, which starts out<br />

lowercase but uses a capital letter where a new word begins. An example of camel notation is<br />

aVeryLongVariable.<br />

It is important not to include spaces around the = when declaring your variables; if you do,<br />

you will get an error.<br />

If you want to make your variable an environmental variable, then you must export it using<br />

the export command. Usually this is done immediately after the variable is declared, which can<br />

be done all on one line like this:<br />

ENVVAR=Value; export ENVVAR<br />

After the shell exports the variable, it will be immediately available for any program or shell<br />

script until the parent shell process exits. (In other words, every time you launch a new shell, you<br />

will need to redeclare and export the variable to use it again.) If you are planning on using an<br />

environmental variable over and over, it may be best to declare it in your .bash_profile file so it<br />

gets declared each time you launch your terminal or a shell.<br />

Argument Variables<br />

Some of the most commonly used variables are ones you don’t need to declare at all. These are<br />

variables passed to the script as arguments from the command line when the script is called. For<br />

example, if you were to run an executable script called ascript, you could pass arguments to the<br />

script when you call it, like this:<br />

leopard:~ scott$ ascript arg1 arg2 arg3<br />

Here the arg1, arg2, and arg3 values are automatically assigned to variables $1, $2, and $3 so<br />

they can be used as needed in the script.<br />

NOTE In many computer languages, counting begins with 0, not 1. Command-line arguments<br />

in scripts are no exception. In scripts, $0 is always the script itself and will return the complete<br />

path name of the executed script.<br />

These special variables don’t have to be passed in from the command line, though. Occasionally<br />

it may be advantageous to create and control these arguments from within the script.<br />

This can be done by using the set command (as we did in our example shell script earlier). The<br />

set command will replace all command-line arguments with the arguments provided.<br />

NOTE set is a fairly complex command in Bash with lots of different capabilities. If you use<br />

set alone on the command line, it will list every environmental variable and function available<br />

to you in a nice, readable format. Used with options, set allows you to toggle many shell<br />

attributes. If you are interested, the gory details of all the possibilities for this command begin<br />

on page 51 of the Bash main page and continue until about three-quarters of the way through<br />

page 53. Also of note, set behaves differently in other shells; for example, in csh you would<br />

use set in place of export to create environmental variables.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!