# contrib/completion/git-completion.bash: declare -g is not portable

6 messages from 2018-02-03 to 2018-02-04. Participants: Torsten Bögershausen, Andreas Schwab, Jeff King, Duy Nguyen, Eric Sunshine, Lucas Werkmeister.
Thread: https://gitlist.dev/t/47751

## Torsten Bögershausen, 2018-02-03 17:20

Subject: contrib/completion/git-completion.bash: declare -g is not portable
Message-ID: <1e618c0c-81ba-fa7e-8039-748bdfa5a6d9@web.de>
URL: https://gitlist.dev/e/1e618c0c-81ba-fa7e-8039-748bdfa5a6d9%40web.de

```
Hej Duy,
After running t9902-completion.sh on Mac OS I got a failure
in this style:

.../projects/git/git.pu/t/../contrib/completion/git-completion.bash: line 300:
declare: -g: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]
--- expected	2018-02-03 17:10:18.000000000 +0000
+++ out	2018-02-03 17:10:18.000000000 +0000
@@ -1,15 +1,2 @@
---quiet
---detach
---track
---orphan=
---ours
---theirs
---merge
---conflict=
---patch
---ignore-skip-worktree-bits
---ignore-other-worktrees
---recurse-submodules
---progress
 --no-track
 --no-recurse-submodules
not ok 92 - double dash "git checkout"

What is "declare -g" good for ?

<https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html#index-declare>

```

## Andreas Schwab, 2018-02-03 19:51

Subject: Re: contrib/completion/git-completion.bash: declare -g is not portable
Message-ID: <87lgg9g8cr.fsf@linux-m68k.org>
URL: https://gitlist.dev/e/87lgg9g8cr.fsf%40linux-m68k.org
In-Reply-To: <1e618c0c-81ba-fa7e-8039-748bdfa5a6d9@web.de>

```
On Feb 03 2018, Torsten Bögershausen <tboegi@web.de> 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.
    
Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

```

## Jeff King, 2018-02-03 20:22

Subject: Re: contrib/completion/git-completion.bash: declare -g is not portable
Message-ID: <20180203202234.GA13374@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20180203202234.GA13374%40sigill.intra.peff.net
In-Reply-To: <87lgg9g8cr.fsf@linux-m68k.org>

```
On Sat, Feb 03, 2018 at 08:51:16PM +0100, Andreas Schwab wrote:

> On Feb 03 2018, Torsten Bögershausen <tboegi@web.de> 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

```

## Duy Nguyen, 2018-02-04 09:45

Subject: Re: contrib/completion/git-completion.bash: declare -g is not portable
Message-ID: <CACsJy8BKpniRxzDa8tOzEu=UVB0kMgv-n0tqZgp0Jreruqq-mg@mail.gmail.com>
URL: https://gitlist.dev/e/CACsJy8BKpniRxzDa8tOzEu%3DUVB0kMgv-n0tqZgp0Jreruqq-mg%40mail.gmail.com
In-Reply-To: <1e618c0c-81ba-fa7e-8039-748bdfa5a6d9@web.de>

```
On Sun, Feb 4, 2018 at 12:20 AM, Torsten Bögershausen <tboegi@web.de> wrote:
> Hej Duy,
> After running t9902-completion.sh on Mac OS I got a failure
> in this style:

Sorry I was new with this bash thingy. Jeff already answered this (and
I will fix it in the re-roll) but just for my own information, what
bash version is shipped with Mac OS?
-- 
Duy

```

## Eric Sunshine, 2018-02-04 09:57

Subject: Re: contrib/completion/git-completion.bash: declare -g is not portable
Message-ID: <CAPig+cQXT1ov4MjzSzqiLBzr4wN1XcP7aSxMP+_dhtWtYwhDAA@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcQXT1ov4MjzSzqiLBzr4wN1XcP7aSxMP%2B_dhtWtYwhDAA%40mail.gmail.com
In-Reply-To: <CACsJy8BKpniRxzDa8tOzEu=UVB0kMgv-n0tqZgp0Jreruqq-mg@mail.gmail.com>

```
On Sun, Feb 4, 2018 at 4:45 AM, Duy Nguyen <pclouds@gmail.com> wrote:
> On Sun, Feb 4, 2018 at 12:20 AM, Torsten Bögershausen <tboegi@web.de> wrote:
>> After running t9902-completion.sh on Mac OS I got a failure
>> in this style:
>
> Sorry I was new with this bash thingy. Jeff already answered this (and
> I will fix it in the re-roll) but just for my own information, what
> bash version is shipped with Mac OS?

The MacOS bash is very old; note the copyright date:

    % /bin/bash --version
    GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
    Copyright (C) 2007 Free Software Foundation, Inc.

A recent bash installed manually (not from Apple):

    % /usr/local/bin/bash --version
    GNU bash, version 4.4.18(1)-release (x86_64-apple-darwin16.7.0)
    Copyright (C) 2016 Free Software Foundation, Inc.

```

## Lucas Werkmeister, 2018-02-04 11:48

Subject: Re: contrib/completion/git-completion.bash: declare -g is not portable
Message-ID: <f8500cbd-dd40-690b-22e9-a9f6a7d1eb8f@lucaswerkmeister.de>
URL: https://gitlist.dev/e/f8500cbd-dd40-690b-22e9-a9f6a7d1eb8f%40lucaswerkmeister.de
In-Reply-To: <CAPig+cQXT1ov4MjzSzqiLBzr4wN1XcP7aSxMP+_dhtWtYwhDAA@mail.gmail.com>

```
On 04.02.2018 10:57, Eric Sunshine wrote:
> On Sun, Feb 4, 2018 at 4:45 AM, Duy Nguyen <pclouds@gmail.com> wrote:
>> On Sun, Feb 4, 2018 at 12:20 AM, Torsten Bögershausen <tboegi@web.de> wrote:
>>> After running t9902-completion.sh on Mac OS I got a failure
>>> in this style:
>>
>> Sorry I was new with this bash thingy. Jeff already answered this (and
>> I will fix it in the re-roll) but just for my own information, what
>> bash version is shipped with Mac OS?
> 
> The MacOS bash is very old; note the copyright date:
> 
>     % /bin/bash --version
>     GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
>     Copyright (C) 2007 Free Software Foundation, Inc.
> 
> A recent bash installed manually (not from Apple):
> 
>     % /usr/local/bin/bash --version
>     GNU bash, version 4.4.18(1)-release (x86_64-apple-darwin16.7.0)
>     Copyright (C) 2016 Free Software Foundation, Inc.
> 

Specifically, Apple ships the latest version of Bash 3.x, which is the
last version published under the GPLv2+ – Bash 4.x switched to GPLv3+.
Users can install newer Bash versions themselves, e. g. using Homebrew,
but that doesn’t help us here.


```
