2015-10-20 36 views
0

這是很基本的shell別名的git的一個例子使用bash腳本egrep的Git的alias命令會產生不同的結果

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

它回聲那些已經合併exluding主分支的分支(和當前分支你上)。很簡單。但是,運行這個別名和腳本時結果不同。例如:

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

出於某種原因,它就像egrep子句甚至無法運行。事實上(因爲我是OS X),它幾乎就像是運行grep命令!像這樣的:(注意grep VS egrep

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

有沒有人碰到這個之前來的呢?

我已經閱讀了這些SO問題,他們沒有處理與我相同的事情。最上面的一個是最接近的,但是與shell命令別名的運行位置(回購的頂層)有關。

Why does running command as git alias gives different results?

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

+0

不相關,但'-L'和'-n'是'xargs'的互斥選項;只使用最後一個。 – chepner

+0

真的!在傳統模式下除外: 「LEGACY DESCRIPTION 在傳統模式下,-L選項將所有換行符視爲行尾,而不管行是空還是以空格結束。此外,-L和 - n個選項不是相互排斥的。「 [手冊頁](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/xargs.1.html) – wspurgin

回答

2

看來,別名定義進行定義前殼處理(或至少反斜槓處理)的一些量。將另一個另一個設置爲別名的轉義反斜槓。

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

謝謝!我還發現刪除第二個反斜槓: '\\ * | master' – wspurgin

相關問題