Operating System - HP-UX
1833386 Members
3387 Online
110052 Solutions
New Discussion

Re: Dynamically Change Environment Variable

 
SOLVED
Go to solution
Tom Dawson
Regular Advisor

Dynamically Change Environment Variable

Hi,

In the Korn Shell, is there a method to dynamically change an environment variable based on another variable's changing?

I'm trying to use this:

BUD=/oravol/u3/dbbackup_${ORACLE_SID}/backups
export BUD

Where BUD would change values if ORACLE_SID changed. i.e. BUD needs to be re-evaluated each time it is referenced.

The single quotes make this happen for the PS1 environment variable:

PS1='${MYHOST} ${PWD} $ '
export PS1

I suspect that is a special case.

Any ideas?

TIA,
Tom
6 REPLIES 6
Michael Steele_2
Honored Contributor

Re: Dynamically Change Environment Variable

Use:

BUD=/oravol/u3/dbbackup_$ORACLE_SID/backups

No parens.
Support Fatherhood - Stop Family Law
James R. Ferguson
Acclaimed Contributor
Solution

Re: Dynamically Change Environment Variable

Hi Tom:

You can do something like the following. I'll use the shell's SECONDS variable in this example:

# BUD='echo /oravol/u3/dbbackup_${SECONDS}/backups'

# eval ${BUD}

Those are single quotes in the variable assignent. Every time the 'eval' is executed, the current number of seconds (since the shell began executing) is displayed.

Regards!

...JRF...
Tom Dawson
Regular Advisor

Re: Dynamically Change Environment Variable

Michael,

Thanks. But that's what I had been trying and it didn't work.

James,

Thanks, that works. The "eval" could be a little cumbersome for our developers. I'm going to leave the Bunny off for a while to see if I can get any other suggestions.

Thanks again,
Tom
James R. Ferguson
Acclaimed Contributor

Re: Dynamically Change Environment Variable

Hi Tom:

To be clear, it is best to get in the habit of using curly braces when doing parameter substitution!

Consider:

# VAR=tom
# echo $VAR_was_here
sh: VAR_was_here: Parameter not set.

Now:

# VAR=tom
# echo ${VAR}_was_here
tom_was_here

As noted in the manpages for 'sh-posix', "Braces are required when parameter is followed by a letter, digit, or underscore that should not be interpreted as part of its name or when a named parameter is subscripted."

Regards!

...JRF...
Tom Dawson
Regular Advisor

Re: Dynamically Change Environment Variable

James,

Thanks.

Yea, I almost always use the curley braces. I find myself forgetting occaisionally, but I've been doing that for 10 - 15 years.

Thanks again,
Tom
James R. Ferguson
Acclaimed Contributor

Re: Dynamically Change Environment Variable

Hi Tom:

My pointer regarding the use of curly braces was based upon the fact that you gave apparent credence to dropping them as a potential solution to your original query. The use of curly braces in parameter substitution is a very valuable habit to develop, in my opinion.

Regards!

...JRF...