git branch
使用git branch命令時治筒,當前分支前會有“*”號顯示需五,怎么通過腳本獲取到呢薪铜,stackoverflow上有個回答使用到了sed命令:
The * is expanded, what you can do is use sed instead of grep and get the name of the branch immediately:
branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
And a version using git symbolic-ref, as suggested by Noufal Ibrahim
branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
How to get the name of the current git branch into a variable in a shell script? [duplicate]
sed命令
sed是一種流編輯器漆弄,它是文本處理中非常中的工具脚草,能夠完美的配合正則表達式使用,功能不同凡響披坏。處理時态坦,把當前處理的行存儲在臨時緩沖區(qū)中,稱為“模式空間”(pattern space)棒拂,接著用sed命令處理緩沖區(qū)中的內(nèi)容伞梯,處理完成后,把緩沖區(qū)的內(nèi)容送往屏幕帚屉。接著處理下一行谜诫,這樣不斷重復(fù),直到文件末尾攻旦。文件內(nèi)容并沒有 改變喻旷,除非你使用重定向存儲輸出。Sed主要用來自動編輯一個或多個文件牢屋;簡化對文件的反復(fù)操作且预;編寫轉(zhuǎn)換程序等。
https://man.linuxde.net/sed