Tuesday, April 03, 2001 12:12 PM TechTalk #1: Setting Environment Variables First of all, allow me start this very first TechTalk by extending a warm welcome to all Macintosh users. As a long-time member of the *BSD tribe, I wish to greet all Mac users about to embrace Mac OS X (a BSD derivative): Welcome to a real operating system! Welcome to a new world of unprecedented stability, powerful flexibility, and networking compliance! On the down side, it took me about 2 years to become a decent Unix user. I can only hope Mac OS X manages to lower that learning curve for all of you. :> SETTING ENVIRONMENT VARIABLES One common problem faced when running Java applications on a Unix system is getting the CLASSPATH right. As I understand it, this is even one of the root reasons InstallAnywhere was created. So, how do you set/correct the CLASSPATH to get that spiffy Java app to start on your new Mac OS X system? There are two ways, depending on the breed of the shell you happen to be using: 1- the Bourne family of shells uses a regular variable assignment, followed by "export" 2- the C-shell family uses "setenv" Note that both shells use "unset" to erase (forget) a variable, should you ever need to do that. SHELL? WHAT SHELL? But first, how do you know what shell is yours? "echo $SHELL" should give you a clue. E.g. (the '%' sign is the shell's prompt): % echo $SHELL /usr/local/bin/zsh % echo $SHELL /opt/contrib/bin/bash % echo $SHELL /bin/sh These three examples, taken from 3 different Unix systems, list "zsh", "bash" and "sh" respectively. THE BOURNE FAMILY OF SHELLS The Bourne family derives from the original Unix shell, simply named "sh". Modern shells based on the original Bourne shell include bash, zsh, ksh. The Bourne shell family is of the opinion that an environment variable is a variable first, a bit of environment next. So you will assign a value to any variable, then "export" it into the environment. Assigning a value to a variable is deceptively easy: % abc=123 % mY_vAR_MixEdCasE="asdf asdf asdf" Note the absence of spaces around the '=' sign. It wouldn't work otherwise; shells are quite finicky about the quotes, spaces, case, etc. If the value contains any spaces, it must be quoted. As it's way too easy to forget, I advise you to always quote everything with double quotes. % mY_vAR_MixEdCasE="asdf asdf asdf" The value of a variable can be recalled by prepending a dollar sign to the variable's name. E.g. : % my_var=word % MY_ENV_VAR="this is a $my_var" % echo $MY_ENV_VAR this is a word Once your variable has been assigned a value, it can be exported into the environment with "export". % export MY_ENV_VAR That's all there is to it. The following example prepends "/usr/local/java" to the CLASSPATH: % CLASSPATH="/usr/local/java:$CLASSPATH" % export CLASSPATH Next, the C shell. THE C SHELL FAMILY The C shell family has been popular with programmers frustrated by the limitations of the original Bourne shell. The first C shell is "csh", a newer incarnation is named "tcsh". Tcsh, along with bash, is one of the most popular shells today. The C shells use a specific command to set an environment variable, "setenv". It does NOT use an equal sign. For example: % setenv MY_ENV_VAR "this is a random value" % setenv CLASSPATH "$CLASSPATH:/usr/my_java_path" Same warning about the quotes: stick to double-quotes, and case matters. HOW TO TEST WHAT YOU'VE JUST DONE "Env" is your friend. Env is a standard Unix command to prints out the content of the environment to your terminal window. E.g.: % env PATH=/home/renaud/bin:/bin:/sbin:/usr/bin:/usr/sbin MAIL=/home/renaud/Maildir USER=renaud LOGNAME=renaud HOME=/home/renaud SHELL=/usr/local/bin/zsh TERM=vt100 MY_ENV_VAR=this is a random value PWD=/home/renaud OLDPWD=/home/renaud VISUAL=vi EDITOR=vi Unfortunately, if your environment is large (it does tend to get so pretty quickly), the list will scroll and you'll be left trying to fetch your variable out of that list visually. I advise you to make use of another staple of the Unix diet, "grep". % env | grep MY_ENV_VAR MY_ENV_VAR=this is a random value That's it! THANKS This TechTalk was suggested by Eric Shapiro. I hope you found it as useful as Eric hoped it would be. Let me know if you have comments/suggestions about this article! --Renaud Waldura