bug-bash
[Top][All Lists]
Advanced

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

Re: Strange problem with source


From: Bert Barbe
Subject: Re: Strange problem with source
Date: Sun, 12 Jan 2014 22:42:41 -0800
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130807 Thunderbird/17.0.8

I think what happens is the following:

"Using declare limits the scope of the variable."
Since the source of b happens inside the function includeFile, the declare of testVariable happens inside that function, and it's scope is limited to that function.

Kind regards,
Bert

On 01/12/2014 01:31 PM, Cecil Westerhof wrote:
Is this a known problem?

I have a strange problem with source. Sometimes variables are not
defined, but functions are.

I have a file a with:
     function includeFile() {
         local fileName
         local needsToExist=true

         if [[ ${1} == "--notNeeded" ]] ; then
             needsToExist=false; shift
         fi
         if [[ ${#} -ne 1 ]] ; then
             echo "includeFile [--notNeeded] <INPUT_FILE>"
             return 1
         fi
         INPUTFILE=${1}; shift

         if  [ -s ${INPUTFILE} -a -f ${INPUTFILE} -a -r ${INPUTFILE} ] ; then
             source ${INPUTFILE}
         else
             if [[ ${needsToExist} != false ]] ; then
                 echo "${INPUTFILE} could not be used"
                 return 1
             fi
         fi
     }

     includeFile b

In file b I have:
     declare testVariable="testing"
     echo ${testVariable}

     function testFunction {
         :
     }

When doing:
     source a

The string "testing" is printed and the function testFunction is
defined, but the variable testVariable is not defined.

When I change in file a the includeFile to source, testVariable is
defined.

Very strange indeed. Especially there includeFile does a source.

I am using bash 4.2.42(1)-release.





reply via email to

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