git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Andreas Schwab <schwab@linux-m68k.org>
Cc: "Torsten Bögershausen" <tboegi@web.de>,
	"Nguyen Thai Ngoc Duy" <pclouds@gmail.com>,
	"Git Mailing List" <git@vger.kernel.org>
Subject: Re: contrib/completion/git-completion.bash: declare -g is not portable
Date: Sat, 3 Feb 2018 15:22:34 -0500	[thread overview]
Message-ID: <20180203202234.GA13374@sigill.intra.peff.net> (raw)
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

  reply	other threads:[~2018-02-03 20:22 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-03 17:20 contrib/completion/git-completion.bash: declare -g is not portable Torsten Bögershausen
2018-02-03 19:51 ` Andreas Schwab
2018-02-03 20:22   ` Jeff King [this message]
2018-02-04  9:45 ` Duy Nguyen
2018-02-04  9:57   ` Eric Sunshine
2018-02-04 11:48     ` Lucas Werkmeister

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180203202234.GA13374@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=pclouds@gmail.com \
    --cc=schwab@linux-m68k.org \
    --cc=tboegi@web.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).