[CentOS] bash variable expansion moment

Mon Nov 16 14:49:33 UTC 2009
Todd Denniston <Todd.Denniston at tsb.cranrdte.navy.mil>

ken wrote, On 11/14/2009 07:37 PM:
> It's half a nice Saturday later and many attempts have brought no
> satisfaction.  Maybe this can't be done.
> 
> 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).
> 
<SNIP>
> 
> 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.
> 
> Any mavens got the skinny on this?

As I understand the variable is interpreted from the perspective of the line of the file, and bash 
does not inline the function.

A trick around it can be gotten with the following modification of your scripts.
---func-file----------------------
Line()
{
echo This is line "$MyLN" $@
}
#extra
#lines
#desired
#to
#show
#that
#execution
#not
#early, orig
#simply
#placed
#early
#in
#file
LineO()
{
echo This is line "$LINENO" $@
}
-------------------------

----main---------------------
#!/bin/bash

. ./func-file

MyLN=$LINENO Line ... it should be $LINENO
LineO ... it should be $LINENO
-------------------------

-- 
Todd Denniston
Crane Division, Naval Surface Warfare Center (NSWC Crane)
Harnessing the Power of Technology for the Warfighter