Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Strange problem with source

5 views
Skip to first unread message

Cecil Westerhof

unread,
Jan 12, 2014, 4:29:19 PM1/12/14
to
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 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.

I am using bash 4.2.42(1)-release.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Cecil Westerhof

unread,
Jan 12, 2014, 6:48:33 PM1/12/14
to
Op Sunday 12 Jan 2014 22:29 CET schreef Cecil Westerhof:
The problem was that I used declare. includeFile is a function, so
that makes the variable local. Removing the declare solved the
problem.
0 new messages