Scope of Variables in sh

Joseph Koshy joseph.koshy at gmail.com
Thu Jun 1 08:39:08 PDT 2006


> but this doesnt function right. so i see the behavior from
> $myline in the while-loop like an local variable......

If you are looking for function local variables,
use the "local" keyword.  For example:

++++ a.sh ++++
a=1
f()
{
        local a
        a=2
        echo B: In f: $a
}
echo A: Outside f: $a
f
echo C: Outside f: $a
++++++++

% sh a.sh
A: Outside f: 1
B: In f: 2
C: Outside f: 1

-- 
FreeBSD Volunteer,     http://people.freebsd.org/~jkoshy


More information about the freebsd-stable mailing list