get env variable from last script

I have 2 scripts t2.sh calls t1.sh. I need to get the vaule of a env variable from t1.sh

/tmp/test$ cat t1.sh 
#!/bin/sh
INSTANCE="font/fc-cache"
export INSTANCE
svcadm disable  ${INSTANCE}
 
/tmp/test$ cat t2.sh 
#!/bin/sh
. /tmp/test/t1.sh
echo ${INSTANCE}

The above works fine on the condition that i know the env variable name
But if the env name is unknown, but can be got by grep "svcadm"

 
/tmp/test$ cat t2.sh 
#!/bin/sh
. /tmp/test/t1.sh
SVCNAME=`grep svcadm t1.sh| awk '{ print $3}'`
echo $SVCNAME

the above script doens't work, it ouputs ${INSTANCE} instead of the real value

So how to get the real value?

temp=$( grep svcadm t1.sh )
var=${temp##* \${}
eval "$( grep "${var%\}}" t1.sh )"

:b:

Thanks alot, that almost worked, it ran the svcadm enable command instead of print the value ${INSTANCE} and it seems work for bash only, Can you write sh version to get value of ${INSTANCE} only, I need the value to check it's status with another cmd e.g 'svcs ${INSTANCE}?

The last 2 expressions are hard to underdstand, Can you explain in details?

It will work in any standard UNIX shell.

On some systems, /bin/sh is an old Bourne shell. Such systems should also have a POSIX shell, e.g. ksh.

See Parameter Expansion in your shell man page.

Ok, I will check Parameter Expansion later, Can you give a quick solution to get the value of ${INSTANCE} only, because your script seems ran the cmd "svcadm disable ${INSTANCE}"

eval "$( grep INSTANCE= t1.sh )"

No, it doesn't.

it didn't work, let me put in the other way

 
/tmp/test$ cat t3.sh 
#!/bin/bash
INSTANCE="font/fc-cache"
var=\$\{INSTANCE\}
echo $var
 
/tmp/test$ ./t3.sh 
${INSTANCE}
 

it outputs ${INSTANCE}, but i need outputs "font/fc-cache".

don't change this:"var=\$\{INSTANCE\} and i don't know the name INSTANCE.

that is how i made it work by creating new file, but it is stupid, any decent solution?

 
:/tmp/test$ cat t3.sh 
#!/bin/bash
INSTANCE="font/fc-cache"
var=\$\{INSTANCE\}
echo "echo $var" >t4.sh
. ./t4.sh
 
$ ./t3.sh 
font/fc-cache

What does "didn't work" mean? What is the output of:

printf "%s\n" "$INSTANCE"

The code I posted doesn't output anything. What did you add to produce output?

thanks, maybe you didn't understand my meaning, but anyway i have made it work by assigning to new variable by eval

 
/tmp/test$ cat t3.sh 
#!/bin/bash
INSTANCE="font/fc-cache"
var=\$\{INSTANCE\}
eval x=$var
echo $x
/tmp/test$ ./t3.sh 
font/fc-cache

That doesn't bear any resemblance to what you have been asking.

It is also redundant.

Why do you not just echo $INSTANCE?:

#!/bin/bash
INSTANCE=font/fc-cache
echo "$INSTANCE"

That does exactly the same as your bloated code.