git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Weird behavior of shell variables in git aliases

From
Jeff King <peff@peff.net>
Date
Mar 22, 2011, 18:32 UTC
Message-ID
<20110322183201.GA22534@sigill.intra.peff.net>
In-Reply-To
<7vfwqfkphn.fsf@alter.siamese.dyndns.org>
On Tue, Mar 22, 2011 at 10:57:08AM -0700, Junio C Hamano wrote:
Show 31 quoted lines
> The first addition is indeed a huge improvement.
> 
>     Note that any argument you pass when running aliases are simply
>     appended to the shell command.
> 
> The original didn't explicitly say it but it really should have.  The
> example that comes before it, "alias.new = !...", should be updated with
> an invocation that takes a parameter, perhaps like this:
> 
>     With this alias defined:
> 
> 	[alias] since = "!gitk --all --since"
> 
>     you can view commits in the last week with:
> 
>         $ git since 7.days
> 
>     because this expands to "gitk --all --since 7.days" by concatenating
>     the arguments supplied at runtime to the alias.
> 
> Then say that "Note ..." to stress that point.  The description at that
> point has become much better.
> 
> With that understanding already there,
> 
>     This means that "alias.foo = !echo $# args: $1, $2 and $3" will
>     not do what you expect.
> 
> is no longer true; nobody sane would expect that if you made them realize
> that "simply appended" already.  Just dropping that sentence would make
> the resulting text flow much better.
Agreed, your version is better.
Show 16 quoted lines
>     If you want to refer to arguments given to the alias, you can
>     wrap it as a shell script, e.g.
> 
> 	[alias] reversed = "!sh -c 'echo $2 $1' -"
> 
>     or a shell function, e.g.
> 
> 	[alias] reversed = "!reversed() { echo $2 $1 } && reversed"
> 
>     and invoke it like so:
> 
> 	$ git reversed one two
>         two one
> 
> I personally think the former "sh -c <str> -" is the more traditional and
> well understood form (iow, an idiom) for people who breathe shells.

Yeah, that is probably true. One advantage of the function idiom is that it doesn't happen inside single-quotes, so it's one less layer of quoting to deal with. And of course it saves a shell invocation. So I think mentioning both is reasonable.

Show 7 quoted lines
> > +----------------------
> > +alias.foo = !echo $# args: $1, $2 and $3
> > +----------------------
> 
> While I totally agree with the formatting advice you gave here, we may
> want to avoid this notation; neither "git config alias.foo = !echo ..."
> nor writing "alias.foo = ..."  in .git/config file would work.
Yeah, I didn't even think about that, but you are right.
-Peff
Previous: Junio C HamanoNext: Lasse Makholm
Message 16 of 20 in “Weird behavior of shell variables in git aliases”
  1. Dun PealMar 21, 2011
  2. Jeff KingMar 21, 2011
  3. Junio C HamanoMar 21, 2011
  4. Junio C HamanoMar 21, 2011
  5. Lasse MakholmMar 22, 2011
  6. Jeff KingMar 22, 2011
  7. Lasse MakholmMar 22, 2011
  8. Ævar Arnfjörð BjarmasonMar 22, 2011
  9. Jeff KingMar 22, 2011
  10. Jeff KingMar 22, 2011
  11. Lasse MakholmMar 22, 2011
  12. Jeff KingMar 22, 2011
  13. Lasse MakholmMar 22, 2011
  14. Dun PealMar 22, 2011
  15. Junio C HamanoMar 22, 2011
  16. Jeff KingMar 22, 2011
  17. Lasse MakholmMar 22, 2011
  18. Junio C HamanoMar 23, 2011
  19. Junio C HamanoMar 22, 2011
  20. Junio C HamanoMar 22, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.