On Sat, Feb 03, 2018 at 08:51:16PM +0100, Andreas Schwab wrote:
> On Feb 03 2018, Torsten Bögershausen <[email protected]> wrote:
>
> > What is "declare -g" good for ?
>
> -g create global variables when used in a shell function;
> otherwise
> ignored
>
> When used in a function, `declare' makes NAMEs local, as with the `local'
> command. The `-g' option suppresses this behavior.
I think the bigger question is why one would use "declare -g" instead of
just assigning the variable with "var=value".
Glancing at the code, I suspect it is because the name of the variable
itself needs expanded. If that's the case, we could use eval instead,
like:
diff --git a/contrib/completion/git-completion.bash
b/contrib/completion/git-completion.bash
index 3cc815be0d..204d620ff7 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -297,7 +297,7 @@ __gitcomp_builtin ()
eval "options=\$$var"
if [ -z "$options" ]; then
- declare -g "$var=$(__git ${cmd/_/ } --git-completion-helper)"
+ eval "$var=\$(__git \${cmd/_/ } --git-completion-helper)"
eval "options=\$$var"
fi
-Peff