oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

bash on Mac OS X
Pages: 1, 2

Regular Variables

A regular variable typically holds a piece of information that you use frequently. Because variables in shell scripts are loosely typed, unlike C and Java, variables can hold virtually anything: a number, string, or composite value such as an array or hash. Creating a variable is pretty painless: name=value, and retrieving its value is almost as easy: $name.

Thus, if you wished to see the current date when opening a new Terminal window, you could place the following lines in your profile:

mydate=`date "+%H:%M:%S %m/%d/%y"`
echo "hi $USER, the current time is $mydate"

For more information on shell scripting, refer to Chris Stone's series of articles mentioned in the introduction.

Environment Variables

Environment variables are essentially a special case of shell variables; they are visible to all child processes that are spawned from the shell. In layman's terms, this means that applications and scripts that are executed from within shell can read the value of the shell's environment variables; as a result, command-line tools typically use environment variables for configuration settings.

By convention, the names of environment variables are all uppercase. Environment variables on OS X may include the following:

Variable Name Description Example
PATH A list of colon-separated directories where the shell will look for executable files /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
CLASSPATH A list of colon-separated directories and/or JAR files that contain Java class files that may be required in the Java Runtime Environment. /Users/dave/FVL/Java/xerces.jar:
JAVA_HOME The root directory of your Java installation. /Library/Java/Home
PWD Your current directory in the file system. /Applications
USER Your login name. dave

To find out which environment variables have been defined in your shell, use the env command, which simply echoes all the environment variables to the terminal:

All environment variables defined in my shell.
Figure 4: All environment variables defined in my shell.

Creating new environment variables is pretty simple; bash uses the syntax export VAR_NAME=value. Thus any shell variable can be turned into an environment variable via the export command:

# create the JAVA_HOME environment variable
export JAVA_HOME=/Library/Java/Home

# do the same thing explicitly, using two lines
export JAVA_HOME

And what happens if a required environment variable doesn't exist? Nothing too drastic -- the kernel won't panic and your computer won't start blowing smoke. It shouldn't, anyway. The shell and related tools require certain variables to be defined (such as ), but other tools may need their own environment variables. In the event that you are missing a required variable, chances are the related program will fail safely and let you know what needs to be defined, as does Apache's fop:

A typical example of missing an environment variable.
Figure 5: A typical example of missing an environment variable.

The following table lists the differences in working with variables (both shell and environment variables) between tcsh and bash on OS X:

Command tcsh bash
Display all shell variables set set
Create a shell variable set name=value name=value
Remove a shell variable unset name unset name
List all environment variables env env
Create an environment variable setenv name value export name=value
Remove an environment variable unsetenv name unset name


Aliases have been a part of Mac OS for several generations now. However, the terminology has gotten a bit confusing with Mac OS X since it's a branch between two traditionally distinct family trees: Unix and Mac OS, each with their own vocabulary. So it shouldn't be too much of a surprise that bringing the two together results in some conflict. One example of this is the term alias: the Finder and Unix underbelly each use the term for two different things:

  • In the Finder, aliases make it convenient to access a file that is somewhere else on your hard drive; an alias essentially allows you to access the same file from multiple places.
  • Aliases in shells serve a similar purpose, but because they exist in a text-only environment, their role is a little different (the Unix equivalent of the Finder's alias is called a link); shell aliases allow you to remap virtually any Unix command to another name, and the aliased commands can then be invoked using their new name.

Aliases are declared in a similar method to variables, with the keyword alias in front of the declaration: alias name=command. If the command contains spaces, then you will have to wrap it in double quotes. To find out which aliases you have already defined, simply enter the command alias (in the event that you wish to remove an alias, then you can simply use the command unalias name):

alias l="ls -l"
alias ll="ls -al"
alias ~="cd ~"
alias md=mkdir
alias mckoi="java com.mckoi.runtime.McKoiDBMain "

# remove the alias named md (for demonstration purposes)
unalias md

The following image illustrates the use of the first two aliases created above:

An example of how even simple aliases can save you time at the keyboard.
Figure 6: An example of how even simple aliases can save you time at the keyboard.

Setting Your Prompt

After opening a Terminal window and being greeted by the message of the day, the first thing you'll see is your command prompt, waiting to accept your every wish. If you went ahead and changed your prompt in Jaguar with tcsh, chances are you'll be disappointed when you first see Panther's default prompt, which should appear something like:

Panther's default prompt
Figure 7: The default prompt for bash in Panther.

Like tcsh, however, it is a relatively trivial process to change your prompt in bash by setting the value of two special shell variables, PS1 and PS2. Why two, you may ask? Good question -- PS1 is can be considered to be the primary prompt, which is displayed every time a new command can be entered; while PS2 is the secondary prompt that is displayed when a command spans more than one line. This screen shot illustrates the two prompts in use:

Illustration of PS1 and PS2
Figure 8: An illustration of PS1 and PS2 in bash's default configuration.

The following table lists the information that you may wish to insert into your prompt:

Command Description Example
\a ASCII bell character --
\d the current date Sun Feb 08
\H hostname Ginger.local
\h shortened hostname Ginger
\u your username dave
\w current working directory /Applications/Network
\W basename of the current working directory Network
\T current time (12-hour HH:MM:SS format) 01:16:49
\t current time (HH:MM:SS format) 13:16:49
\@ current time (12-hour AM/PM format) 1:16 PM
\n new line --
\\ print a backslash \

From the information listed in the above table, we can see that Panther's default prompt strings are created using the following commands:

PS1="\h:\w \u$ "
PS2=" > "

Wrapping Up

This article provides an overview on making the transition from tcsh and bash; at this point you should now know enough about bash to start tailoring your Panther environment to your needs. And while this article covers several key features of it, we have barely scratched the surface; command history, job control, scripting, and several other features went untouched. For more information, be sure to check out the following resources:

  • As with most Unix programs, the bash documentation is installed along with the program itself, and it can be found in the man pages. Simply type the command man bash at the prompt to read the pages.
  • The Free Software Foundation's web site hosts the complete manual for the bash shell.
  • Learning the bash Shell, 2nd Edition by Cameron Newham & Bill Rosenblatt (O'Reilly & Associates, 1998) provides an in-depth introduction to the basics of shells, and then goes on to cover everything you would ever need to know about bash.

David Miller is combining his passions of photography and working on the web at iStockphoto; when not hacking away with a text editor and a few web browsers in hand, he can be seen huddled over his laptop tweaking levels and curves for his freelance photography. Keep track of David's latest projects over at his home on the web.

Return to the Mac DevCenter