bug-bash
[Top][All Lists]
Advanced

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

Re: sourcing script file from inside a function doesn't work anymore


From: Geir Hauge
Subject: Re: sourcing script file from inside a function doesn't work anymore
Date: Mon, 12 May 2014 15:33:34 +0200

2014-05-12 14:11 GMT+02:00 Greg Wooledge <wooledg@eeg.ccf.org>:

> On Sat, May 10, 2014 at 12:22:40PM +0200, thioroup8 wrote:
> > Now, I source it from inside a function: I obtain following error
> message:
> > bash: declare: tmp: not found
> > I think this problem raises in the particular case of involving array
> > variables definitions inside sourced bash script file...
>
> >   cat << "EOF" > "${file_to_be_sourced}"
> > declare -a tmp='([0]="1" [1]="2")'
> > EOF
>
> The problem is that you are using "declare" within the file-to-be-sourced.
> When you source the file inside a function, Bash runs the declare command
> within the context of the function, where declare has the same meaning as
> "local".  Thus, it makes the variable local to the function.


There really is a bug here, a weird one. Using a simplified script, we
see that this works as expected:

$ source <(printf "declare -a array='(x)'; declare -p array\n")
declare -a array='([0]="x")'


Wrapping it in a function should yield the same output, but doesn't:

$ f() { source <(printf "declare -a array='(x)'; declare -p array\n"); }; f
bash: declare: array: not found


And while declare -p array fails to "find" the array, declare -p
(without a name) does list it:

$ f() { source <(printf "declare -a array='(x)'; declare -p array; declare
-p | grep array= \n"); }; f
bash: declare: array: not found
declare -a array='([0]="x")'


Oddly, the quotes seem to matter; changing array='(x)' to array=(x)
makes it work...

$ f() { source <(printf "declare -a array=(x); declare -p array\n"); }; f
declare -a array='([0]="x")'

-- 
Geir Hauge


reply via email to

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