Operating System - HP-UX
1833420 Members
3520 Online
110052 Solutions
New Discussion

problem with exported env. var. with index

 
Derko Drukker
Occasional Advisor

problem with exported env. var. with index

in .profile I have:
export TESTVAR=hallo
export TESTV[1]=jaja

in truuk I have:
echo ${TESTVAR}
echo ${TESTV[1]}

I have this on two (identical) systems.

sh truuk on system A gives
hallo
jaja
but on system B it gives
hallo

So on system B ${TESTV[1]} is not known in the subshell.
The variables are known in the current shell.
It used to work on system B also, but after a reboot it doesn't anymore.

Any clues ?


1 REPLY 1
A. Clay Stephenson
Acclaimed Contributor

Re: problem with exported env. var. with index

exporting of arrays is never going to work. You really have to realize that the environment is simply an array of strings.

You can convince yourself of this by:
ARRY[0]=Zero
ARRY[1]=One
ARRY[2]=Two
PLAINVAR=Plain
export ARRY[0] ARRY[1] ARRY[2]
# now spawn a child shell
sh
# now examine the enviroment
env | pg
# You will see only ARRY=Zero
# The subscript will be gone
# You will also see PLAINVAR=Plain

If the values are not in the environment then they certainly can't be echo'ed
Now as a surprise while still in the child shell:

echo "${ARRY[0]}"
echo "${ARRY}"
echo "${PLAINVAR}"
echo "${PLAINVAR[0]}"

You will find that all of these work!

Now you could do something like this (with the original values of ARRY in the parent shell):
FAKEARRY="${ARRY[*]}"
export FAKEARRY

Now in the child process, you could
process ${FAKEARRY} to build a new array.

rebuild_array()
{
typeset -i KNT=0
while [[ ${#} -ge 1 ]]
do
ARRY[${KNT}]=${1}
shift
((KNT += 1))
done
return(0)
} # rebuild_array

rebuild_array ${FAKEARRY}
# Now in the child you should have
# ARRY[0], ARRY[1], ...


If it ain't broke, I can fix that.