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, 13:43 UTC
Message-ID
<20110322134358.GA19064@sigill.intra.peff.net>
In-Reply-To
<AANLkTin3fXkGaC5cTVny5adU=FusQV0WAcPHLUEeEzLi@mail.gmail.com>
On Tue, Mar 22, 2011 at 02:35:10PM +0100, Lasse Makholm wrote:
Show 30 quoted lines
> On 22 March 2011 14:28, Jeff King <peff@peff.net> wrote:
> > But I think that is a little too magic for my taste. Although the false
> > positives ("!echo 'literal $#'") and false negatives (you want "!foo" to
> > _ignore_ its parameters) are pretty obscure, I would prefer to keep
> > things simple.
> 
> Then how about simply:
> 
> diff --git a/Documentation/config.txt b/Documentation/config.txt
> index 6468a68..8097480 100644
> --- a/Documentation/config.txt
> +++ b/Documentation/config.txt
> @@ -586,9 +586,16 @@ If the alias expansion is prefixed with an
> exclamation point,
>  it will be treated as a shell command.  For example, defining
>  "alias.new = !gitk --all --not ORIG_HEAD", the invocation
>  "git new" is equivalent to running the shell command
> -"gitk --all --not ORIG_HEAD".  Note that shell commands will be
> -executed from the top-level directory of a repository, which may
> -not necessarily be the current directory.
> +"gitk --all --not ORIG_HEAD". Note that any arguments you pass
> +when running aliases are simply appended to the shell command.
> +This means that "alias.foo = !echo $# args: $1, $2 and $3" will
> +not do what you expect. To use alias arguments as positional
> +parameters, wrap your command in a shell function:
> +"alias.foo = !foo () { echo $# args: $1, $2 and $3; }; foo"
> ++
> +Shell commands will be executed from the top-level directory
> +of a repository, which may not necessarily be the current
> +directory.
Yeah, that certainly improves the situation.

A small formatting nit: Those long commands with punctuation get hard to read in the middle of a paragraph. Maybe something like this on top:

diff --git a/Documentation/config.txt b/Documentation/config.txt
index 3916665..d2b7515 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -582,10 +582,18 @@ it will be treated as a shell command.  For example, defining
 "git new" is equivalent to running the shell command
 "gitk --all --not ORIG_HEAD". Note that any arguments you pass
 when running aliases are simply appended to the shell command.
-This means that "alias.foo = !echo $# args: $1, $2 and $3" will
-not do what you expect. To use alias arguments as positional
+This means that
++
+----------------------
+alias.foo = !echo $# args: $1, $2 and $3
+----------------------
++
+will not do what you expect. To use alias arguments as positional
 parameters, wrap your command in a shell function:
-"alias.foo = !foo () { echo $# args: $1, $2 and $3; }; foo"
++
+----------------------
+alias.foo = !foo () { echo $# args: $1, $2 and $3; }; foo
+----------------------
 +
 Shell commands will be executed from the top-level directory
 of a repository, which may not necessarily be the current

-Peff

PS Your patch was wrapped on the @@ line. You might want to check your
   mailer settings.
Previous: Lasse MakholmNext: Lasse Makholm
Message 12 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.