Using Environment Variable

In our current environment we have each of our testing levels on individual servers (running Korn shell). So, there is a server for dev and test, and 2 servers for qa and prod. I have several scripts that utilize a code that is dependant on the server where it resides. While I was the only person using these scripts, I had that value hard coded in my scripts (they were quick and dirty scripts). Now we have several folks that will be supporting this and I want to keep the scripts in a central repository and change this value to be an environment variable.

My thought is to set this as an environment variable in the users .profile. Then the scripts will use that variable, instead of having to change it for each server/test level.

My assumption, and from everything I've read, is that if I export the variable in my profile, it should be available to scripts when executed. But, that is not what I am seeing. And these scripts are executed by the user from the command line.

Here is an example. Any help would be appreciated.

.profile:
VAR=server_a
export $VAR

script:
some_command $VAR #executes command passing in $VAR

When I do this, $VAR does not seem to contain server_a. It is empty instead. However, if I echo out $VAR at the command line, it does equal server_a.

A solution that does seem to work, is to set the variable in a .kshrc file and execute that in my script prior to using it. But I would prefer to have it set in the profile and have it available for the scripts.

Any help would be appreciated. Thanx

It is:
export var
not
export $var

But do you need that? The boxes should know their own names. I just use the hostname command when I want the hostname. "uname -n" is another possibility.

/etc/profile and $HOME/.profile are sourced for login (Bourne compatible) shells.
That's probably why you see the exported variables set when you echo them from your login shell.
On the other hand if you need special settings for your sub shells as well
then export ENV in your .profile and let it point to an appropiate .shrc file in your HOME
Refer to the FILES section in the manpage of your shell.
Also note that if your script is for instance run from a cron job that it only gets a pretty rudimentary environment (see man crontab).
As for host or node names,
I often have such an export in my .profile
(you may have to separate assignment and export to two lines, and use backticks
if you don't have a Posix shell)

export NODENAME=$(/usr/bin/uname -n)

OK, I feel like a moron. I dropped the $ from my export and it works fine. I should have caught that.

Thanks for the replies.