Wednesday, August 15, 2007

Environment variables set in shell script not available after shell script

Problem
I struggled for hours trying to figure out why my shell script for setting my Java classpath wouldn't work. It was pretty straightforward too...

CLASSPATH=$CLASSPATH:/home/xxx/lib/itext.jar:.
export CLASSPATH

Then I ran it like this:

$ setclasspath.sh

And my classpath would be the same as it was before the script.

Solution
I needed to call it with the "." notation, as in:

$ . setclasspath.sh

This notation indicates that the settings should be applied to the current shell versus starting a new shell to execute the script, which was what I was getting by invoking by specifying the name.

No comments: