Why use export? (Was Re: shell vs csh)

Jeff Beadles jeff at quark.WV.TEK.COM
Thu Mar 29 02:01:08 AEST 1990


rbottin at atl.calstate.edu (Richard John Botting) writes:
>
>Try inserting "; export MANPATH" after "MANPATH=/twolf7/dchen/man"
>
>If that makes it weork the explanation is:
>	'sh'  doesn't hand on variables to a subthing
>	unless you tell it to with an
>	export command...
>	PLUS
>	'[ -f $MANPATH/"$1".1 ]' is actually 'test -f $MANPATH/"$1".1'
>
>
>
>Dick Botting, 
>Department of computer science,
>California State University, San Bernardino, CA 92407
>
>rbottin at atl.calstate.edu
>>INTERNET:rbottin at atl.calstate.edu (Compuserve)
>paaaaar at calstate.bitnet 

This is not correct.  The only reason that you would need to export the
variable "MANPATH" is if you wanted to use it as an environmental variable
in a child program/process.  When you call a program (IE: test) with
arguements on the command line, the shell WILL and does expand them
before calling the program.

Take the following example that passes shell variables as command arguements:

-----------------cut here for look--------------------------
#!/bin/sh

echo "\$1 is $1"
exit 0

-----------------cut here for try--------------------------
#!/bin/sh

./look			# No $VAR here.

VAR='Hello-world'

./look $VAR		# Here it is now.

export VAR

./look $VAR		# It's still here.

exit 0

---------------------End of sample scripts-------------------

Notice the output of the scripts:


Script started on Wed Mar 28 07:52:33 1990
jq:> ./try
$1 is 
$1 is Hello-world
$1 is Hello-world
jq:> exit
script done on Wed Mar 28 07:52:42 1990


Notice the effect of the "export"  (None.)

However, if the program were changed to look like this (And to use environment
variables rather than command line arguements):


-----------------cut here for look2--------------------------
#!/bin/sh

echo "\$VAR is $VAR"
exit 0

-----------------cut here for try2--------------------------
#!/bin/sh

./look2			# Should see nothing for $VAR

VAR='Hello-world'

./look2 $VAR		# Where is $VAR?

export VAR

./look2 $VAR		# There it is.

exit 0

---------------------End of scripts-------------------

Now, notice the output of the scripts:

Script started on Wed Mar 28 07:55:53 1990
jq:> ./try2
$VAR is 
$VAR is 
$VAR is Hello-world
jq:> exit
script done on Wed Mar 28 07:56:02 1990


This is what "export" is all about.


Have fun!

	-Jeff
-- 
Jeff Beadles				jeff at quark.WV.TEK.COM 
Utek Engineering, Tektronix Inc.	+1 503 685 2568
			"Credo quia absurdum"



More information about the Comp.unix.questions mailing list