[CentOS] bash variable expansion moment

Sun Nov 15 23:21:40 UTC 2009
ken <gebser at mousecar.com>

On 11/15/2009 02:22 PM Stephen Harris wrote:
> On Sun, Nov 15, 2009 at 01:50:30PM -0500, ken wrote:
>> The problem is that $LINENO is evaluated in the function definition, and
>> not when called.  So I'm thinking to change "$LINENO" in the function
> 
> No it's not.  Variables are _not_ evaluated when the function is defined;
> they're evaluated at execution. ....

See my example below.

> 
> Is this what you wanted to do?

Stephen, thanks for your reply, but you're not seeing what I want to do.
 Let me post my example once again:

I'm trying to write a function which, when called from one function
execute in another.  In itself, that's not the problem.  Rather, there's
one built-in variable which is evaluated in the function definition and
it's value is then set (too early).

Here's the one file (func-file) with the function definition:
-------------------------
Line()
{
echo This is line "$LINENO" $@
}
-------------------------

That one is called by this one below:
-------------------------
#!/bin/bash

. ./func-file

Line ... it should be $LINENO
------------------------

I want the function Line to show the line number in the second file
where it's executed, not the line number from the sourced function.

I want the output to be:

This is line 5 ... it should be 5

but it's not.  The num output in "This is line [num]" is whatever the
line number is in the function definition.  (I.e., $LINENO is evaluated
in the function.  Try it if you don't believe me.)

What I'm looking for is the proper syntax to wrap around $LINENO in the
function definition (in func-file) so that it's not evaluated there but
is evaluated when the function is called in the second file.