Operating System - HP-UX
1833760 Members
2547 Online
110063 Solutions
New Discussion

env var which is not defined yet

 
Geetam
Frequent Advisor

env var which is not defined yet

I want to set an environment variable, part of the value is the value of another environment variable. But at the time I am setting the first one, the second is not defined yet. How do I do this?

I tried single quotes:

var1='string${var2}'
echo "var2 is not set yet, var1 is: ${var1}"
# the output is: string${var2}
# I expected: string
#
var2="VAR"
echo "now var2 is set, var1 is: ${var1}"
# the output is: string${var2}
# I expected (I want): stringVAR
#
echo "now var2 is set, var1 is: `eval echo ${var1}`"
# this works, but I have no control over all
# the other shell scripts, which use this variable

I thought there was a simple solution... Can you remind me or point me in the right direction, please.
6 REPLIES 6
Alan Riggs
Honored Contributor

Re: env var which is not defined yet

If var2 is not yet defined, you cannot place the value of var2 into another variable.

It might help if you explained the problem you are trying to solve. There might well be an easy solution, but accessing the value of an undefined variable isn't it.
John Palmer
Honored Contributor

Re: env var which is not defined yet

Enclosing a string in single quotes prevents variable substitution.

For example:-
var2=VAR
var1='string${var2}'

var1 will contain string${var2}

Double quotes allows substitution:-
var2=VAR
var1="string${var2}"

var1 will contain stringVAR

Some more information aboutr what you are trying to achieve would be useful.

Regards,
John
Victor BERRIDGE
Honored Contributor

Re: env var which is not defined yet

As Alan suggest you should tell more about what you are trying to perform...
It is true that looking at your problem with the info we have, it can only be done using eval..., and that you cannot use unseted variable (or should not...), so you can test var2 to see if has default value, if not then eval...

Best of all

Victor
Alan Riggs
Honored Contributor

Re: env var which is not defined yet

If it isn't really necessary to have stringVAR be the explicit value within var1, you can get around it with something like:

# var1='string${var2}'
# echo $var1
string${var2}
# var2=VAR
# eval echo ${var1}
stringVAR
Dan Hetzel
Honored Contributor

Re: env var which is not defined yet

Hi Geetam,


It's really bad practice to use a variable which hasn't been defined yet.
I'm afraid that you'll be forced to use 'eval' if you use it anyway.

Could you give us some more information about what you're trying to achieve? There is surely a different way to do what you want.

Dan

Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Geetam
Frequent Advisor

Re: env var which is not defined yet

I think further explanation will make it harder to focus on the question itself, but I will try.

I have a script (a library of functions) which is called by other scripts. At the beginning of this (function library) script, an environment variable is set. As a result of changes in business procedures, this variable needs to be appended with another variable. But this second variable is only set when one of the functions is executed (and of course the whole thing relies on this function to be executed, but that is no problem).

Because the functions are rather complex, I am reluctant to change all the code in the functions (add 'eval') and test all those changes.

I can think of other solutions, e.g. setting the variable inside the function that defines the second variable. But there is a reason why the first variable is set at the top of the script, it changes from time to time and needs to be edited. The second variable is defined by the environment in which the scripts are running, so that one gets its value at runtime.

Using 'eval' in the function that defines the second variable should work, but it did not work when I tried it. I will try that again.

I just asked the question because I thought there was a quick solution using the single quotes. But if it cannot evaluate automatically, I might have to do the hard and dirty work of a Unix system administrator ;-) (that is my job anyway)

Thanks for your help. It is so good to be able to ask a bunch of colleagues and get a quick response.