[Top][All Lists]

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

Re: strangeness about variable inside the shell

From: Paul D. Smith
Subject: Re: strangeness about variable inside the shell
Date: Wed, 4 Jun 2003 02:00:20 -0400

%% "Huiseok Kim" <address@hidden> writes:

  hk> <style> p {margin-top:0px;margin-bottom:0px;} </style><table border=0 
width=100% bgcolor='' cellpadding=0 cellspacing=0 align=center><tr><td 
valign=top style='padding:8pt;'><font size=2><P>it looks&nbsp;strange that the 
variable in the shell doesn't work with builtin function. following is&nbsp;one 
of the situation:</P>

Please don't post HTML.

  hk> list = foo/bar google deep/deeper/deepest
  hk> test:
  hk>       for item in $(list); do \
  hk>           echo $(notdir $$item); \
  hk>       done

  hk> result:

  hk> foo/bar
  hk> google
  hk> deep/deeper/deepest

  hk> i had expected to be listed it as 'bar google deepest'


You must keep very firmly in mind the distinction between the shell and
make.  Make handles resolving all make variables and functions, THEN it
takes the results of that and hands it to the shell, and the shell
executes it.  That's the ONLY interaction between them.

So, make expands the script BEFORE it invokes the shell.  Once the shell
gets the results, there's nothing make can do with it; the shell is a
separate process.

Here you are trying to mix a shell construct (the for loop) and have a
make function (notdir) be invoked each time through the loop; that can't
work.  Make expands the $(notdir $$item) before it runs the shell, so
the shell gets this to run:

    for item in foo/bar google deep/deeper/deepest; do echo $item; done

You need to invoke the make function on the _make_ variable, not the
_shell_ variable; you can do this:

            for item in $(notdir $(list)); do \
                echo $$item; \

Or, if that doesn't do what you need, you'll have to use a _shell_
"function" on the shell variable, not a make function:

            for item in $(list); do \
                echo `basename $$item`; \

 Paul D. Smith <address@hidden>          Find some GNU make tips at:
 http://www.gnu.org                      http://make.paulandlesley.org
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist

reply via email to

[Prev in Thread] Current Thread [Next in Thread]