2015-04-17 63 views
1

考慮以下bash腳本打印:商店彩色輸出,具有可變和顏色

#!/bin/bash 

# with colors 
git status --short 

# without colors 
git_output=$(git status --short) 
echo -n "$git_output" 

這種打印出「?? color_print.sh」兩次,其中??先是紅色,然後是默認顏色。如何可以存儲和打印輸出有?第二行還有顏色嗎?

謝謝!

回答

1

你需要強制git吐出顏色,即使它的輸出沒有去終端(當它檢測到它通常會禁用顏色)。

不幸的是,它看起來不像git status明白--color選項,你需要做到這一點最簡單。

這使您需要手動調整正確的配置設置。

git_output=$(git -c color.status=always status --short) 
echo -n "$git_output"