0

Here's an example of pretty basic shell alias in git

[alias]
    em = "!git branch --merged | egrep -v '\\*\\|master' | xargs -L 1 -n 1 echo"

It echos the branches that are already merged exluding the master branch (and the current branch you're on). Pretty simple. However the results are different when running this alias and just the script. Example:

will ~/example master 
$ git branch --merged | egrep -v '\\*\\|master' | xargs -L 1 -n 1 echo
feature
will ~/example master 
$ git em
feature
*
master

For some reason it's like egrep clause isn't even run. In fact (since I'm OS X) it's almost like it ran the grep command instead! Like this: (note the grep vs egrep)

will ~/example master 
$ git branch --merged | grep -v '\\*\\|master' | xargs -L 1 -n 1 echo
feature
*
master

Has anybody come across this before?

I've read up on these SO questions and they aren't dealing with the same thing I am. The top one is the closest, but has to do with from where shell command alias are run (the toplevel of the repo).

Why does running command as git alias gives different results?

Bash script produces different result when executed from shell prompt than executed by cron

Community
  • 1
  • 1
wspurgin
  • 2,600
  • 2
  • 17
  • 20
  • Unrelated, but `-L` and `-n` are mutually exclusive options for `xargs`; only the last one is used. – chepner Oct 20 '15 at 17:15
  • True! except in Legacy mode: "LEGACY DESCRIPTION In legacy mode, the -L option treats all newlines as end-of-line, regardless of whether the line is empty or ends with a space. In addition, the -L and -n options are not mutually-exclusive." [man pages](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/xargs.1.html) – wspurgin Oct 20 '15 at 19:21

1 Answers1

2

It appears that the alias definition undergoes some amount of shell processing (or at least backslash processing) before it is defined. Add another set of escaped backslashes to the alias.

[alias]
  em = "!git branch --merged | egrep -v '\\\\*\\\\|master' | xargs -L 1 -n 1 echo"
chepner
  • 497,756
  • 71
  • 530
  • 681