使用变量存储彩色输出并使用颜色打印

时间:2015-04-17 13:00:35

标签: git bash variables colors

考虑以下bash脚本:

#!/bin/bash

# with colors
git status --short

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

这两次打印出“?? color_print.sh”,其中??先是红色然后是默认颜色。如何存储和打印输出有??在第二行也有色?

谢谢!

1 个答案:

答案 0 :(得分:1)

你需要强制git吐出颜色,即使它的输出没有到达终端(当它检测到正常时它会禁用颜色)。

不幸的是,git status看起来不了解您最容易做到的--color选项。

这使您需要手动调整正确的配置设置。

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