Discussion:
Bug in nested source?
(too old to reply)
Cecil Westerhof
2014-01-12 20:16:38 UTC
Permalink
I have a file in which I define functions and variables. This file is
sourced in a file that is sourced. After this the functions are known,
but the variables not. If I source the file directly, the variables
are known. I am using bash 4.2.42(1)-release.

Is this a known problem?
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Cecil Westerhof
2014-01-12 21:12:09 UTC
Permalink
Post by Cecil Westerhof
I have a file in which I define functions and variables. This file
is sourced in a file that is sourced. After this the functions are
known, but the variables not. If I source the file directly, the
variables are known. I am using bash 4.2.42(1)-release.
Is this a known problem?
It is a little strange. Just source is not the problem. But it is
certainly peculiar.

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 testVarable 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.
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Cecil Westerhof
2014-01-12 23:32:44 UTC
Permalink
Post by Cecil Westerhof
Post by Cecil Westerhof
I have a file in which I define functions and variables. This file
is sourced in a file that is sourced. After this the functions are
known, but the variables not. If I source the file directly, the
variables are known. I am using bash 4.2.42(1)-release.
Is this a known problem?
It is a little strange. Just source is not the problem. But it is
certainly peculiar.
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
declare testVariable="testing"
echo ${testVariable}
function testFunction {
}
source a
The string "testing" is printed and the function testFunction is
defined, but the variable testVarable 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.
IncludeFile is a function. So with declare the variable becomes local.
Not using declare solves the problem.
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Loading...