Re: Weird behavior of shell variables in git aliases

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Mar 21, 2011 at 09:39:43AM -0700, Dun Peal wrote:

> It seems that a variable is available only once?!  How can the
> following shell session be explained:
> 
>     $ git config alias.test0
>     !echo $1
>     $ git test0 foo
>     foo
>     $ git config alias.test1
>     !echo $1 && echo $1
>     $ git test1 foo
> 
>     foo
>     $ git config alias.test2
>     !BRANCH=$1 && echo $BRANCH && echo $BRANCH
>     $ git test2 foo
> 
>     foo

Because in v1.7.4 and earlier, we literally just tack the arguments
(shell-quoted) onto the end of the string. So your alias ends up
expanding to:

  /bin/sh -c "!echo $1 && echo $1 'foo'"

So the first echo is empty, and then the second one echos foo. And what
you are trying to do doesn't work with a straight alias (at the bottom
I'll show you what you want).

But interestingly, that's _not_ the behavior as of Erik's 7f51f8b
(alias: use run_command api to execute aliases, 2011-01-07), which is in
master but not yet released. With that, we end up executing:

  sh -c 'echo $1 && echo $1 "$@"' 'echo $1 && echo $1' 'foo'

which prints "foo foo". So it is technically a regression. I don't know
how much we care; using positional parameters like this was already
nonsensical, as shown above.

For reference, what you actually want (in either system) is:

  $ git config alias.test1
  !sh -c 'echo $1 && echo $1' -
  $ git test1 foo
  foo
  foo

Make sure to include the "-" (or some other string) which ends up as $0.

-Peff
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]