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