[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: exported shell variables and scope



On Wed, Jul 25, 2001 at 04:27:25PM -0400, Dale Scheetz wrote:

> If I explicity place the variable declaration (without export!) before any
> target in the makefile, this variable is visible to other commands in the
> makefile. Why does "sourcing" those declarations change their scope, or is it
> their placement within a target?

Makefiles are not shell scripts, and make variables are not shell variables
(usually).  Try it:

VAR=value

target:
	@echo make variable: $(VAR)
	@echo shell/environment variable: $$VAR

(make sure those are tabs, of course).  $$ is an escape to pass a single $ to
the shell.

> I've been at the point of being able to make this work for several postings
> now, but I still don't understand well enough to see the big picture. It
> seems I get into this confusion whenever I try to port shell logic to
> makefile logic...

Set a shell variable: VAR=value
Export that variable into the environment: export VAR
Do the first two in one step: export VAR=value
Reference a shell or environment variable: $VAR

Set a makefile variable: VAR=value
Reference a makefile variable: $(VAR)

Shell variables are local to a given shell.  Environment variables are
inherited by child processes, and have nothing to do with any shell.  The shell
simply provides a common syntax for accessing both kinds of variables.  This
leads to some confusion, but it's very convenient.

To add to the confu^H^H^H^H^Hflexibility, make(1) will generally transform
environment variables into make variables when it is called.  This means that
you can reference environment variables with both make variable syntax ($(VAR))
and shell syntax ($VAR), and these will both work, though in subtly different
ways.  See make(Environment) in the info documentation.

-- 
 - mdz



Reply to: