I want to capture the color output from git status in a variable and print it later.
So far I have come up to:
status=$(git status -s)
echo -e "$status"
The above script keeps the newline intact (thx to Capturing multiple line output into a Bash variable) but strips the color from the output.
Is there a way to keep the color in the variable and echo it?
The problem is not that bash strips the color output before storing the text, but that
gitrefuses to produce the colored output int he first place, probably because it can tell that its STDOUT is not a terminal. Many commands do this (e.g.ls). Most of these have an option telling them to use color anyway, for use in precisely this situation (e.g.--colorforls). Consult yourgitdocumentation whether it also has such an override option.