[comp.unix.shell] Passing a var thru a file

afsipmh@cidsv01.cid.aes.doe.CA (06/18/91)

Say i have a file that (just for the sake of this question) contains
just one word:
$ENVAR

Say $ENVAR is an environmental variable

What is it about the shell that prevents interpretation:

#!/bin/sh
var=`cat file`
echo $var     #gives (literally) $ENVAR
echo `echo $var`  #also gives (literally) $ENVAR
-- 
Pat Hertel                 Canadian Meteorological Centre
Analyst/Programmer         2121 N. Service Rd.        
phertel@cmc.aes.doe.ca     Dorval,Quebec             
Environment Canada         CANADA           H9P1J3

rbr@bonnie.ATT.COM (228-4197,ATTT) (06/19/91)

In article <1991Jun18.153222.20836@cid.aes.doe.CA> afsipmh@cidsv01.cid.aes.doe.CA () writes:
>Say i have a file that (just for the sake of this question) contains
>just one word:
>$ENVAR
>
>Say $ENVAR is an environmental variable
>
>What is it about the shell that prevents interpretation:
>
>#!/bin/sh
>var=`cat file`
>echo $var     #gives (literally) $ENVAR
>echo `echo $var`  #also gives (literally) $ENVAR
>-- 
>Pat Hertel                 Canadian Meteorological Centre
>Analyst/Programmer         2121 N. Service Rd.        
>phertel@cmc.aes.doe.ca     Dorval,Quebec             
>Environment Canada         CANADA           H9P1J3

The shell never sees $ENVAR as a variable, only a string of arbitrary
characters.

Try:

var=`cat file`
echo `eval $var`

Bob Rager

wallace@ynotme.enet.dec.com (Ray Wallace) (06/19/91)

In article <1991Jun18.153222.20836@cid.aes.doe.CA>, afsipmh@cidsv01.cid.aes.doe.CA writes...
>Say i have a file that (just for the sake of this question) contains
>just one word:
>$ENVAR
It actualy isn't peculuar to files.

>var=`cat file`
>echo $var     #gives (literally) $ENVAR

Try 
	eval echo $var

---
Ray Wallace		
		(INTERNET,UUCP) wallace@oldtmr.enet.dec.com
		(UUCP)		...!decwrl!oldtmr.enet!wallace
		(INTERNET)	wallace%oldtmr.enet@decwrl.dec.com
---

larry@st-andy.uucp (Larry Martell) (06/19/91)

In article <1991Jun18.153222.20836@cid.aes.doe.CA> afsipmh@cidsv01.cid.aes.doe.CA () writes:
>Say i have a file that (just for the sake of this question) contains
>just one word:
>$ENVAR
>
>Say $ENVAR is an environmental variable
>
>What is it about the shell that prevents interpretation:
>
>#!/bin/sh
>var=`cat file`
>echo $var     #gives (literally) $ENVAR
>echo `echo $var`  #also gives (literally) $ENVAR

You need to tell the shell to re-evaluate the expression. The shell IS 
expanding $var (it equals $ENVAR), but you want it to expand $var, and
then expand it again.  You do this using eval:

#!/bin/sh
var=`cat file`
eval echo $var

-- 
Larry Martell                  "Opinions are like assholes; everybody has one, 
212-668-9478		        but nobody wants to look at the other guys"
uunet!st-andy!larry

felps@convex.com (Robert Felps) (06/19/91)

In <1991Jun18.153222.20836@cid.aes.doe.CA> afsipmh@cidsv01.cid.aes.doe.CA writes:

>Say i have a file that (just for the sake of this question) contains
>just one word:
>$ENVAR

>Say $ENVAR is an environmental variable

>What is it about the shell that prevents interpretation:

>#!/bin/sh
>var=`cat file`
>echo $var     #gives (literally) $ENVAR
>echo `echo $var`  #also gives (literally) $ENVAR
>-- 
>Pat Hertel                 Canadian Meteorological Centre
>Analyst/Programmer         2121 N. Service Rd.        
>phertel@cmc.aes.doe.ca     Dorval,Quebec             
>Environment Canada         CANADA           H9P1J3

I would assume you'll get plenty answers like,

   var=`cat file`
   eval echo $var

but you might as well speed up the script too,

   var=`< file`
   eval echo $var

 or better yet,

   read var < file
   eval echo $var

unfortunately, these only work in the Korn shell and the last one also
works in later versions of the shell (>SVR2).

Hope this helps,
Robert Felps            I do not speak for  felps@convex.com
Convex Computer Corp    Convex and I seldom Product Specialist
3000 Waterview Parkway  speak for myself.   Tech. Assistant Ctr
Richardson, Tx.  75080  VMS? What's that?   1(800) 952-0379