For a git-specific solution, you can force git to provide color through the color.status configuration color.status . To override the configuration entry for this single command, use git -c color.status=always status .
Remember that command output captured this way does not necessarily include a trailing newline, so you want to add that if you plan to print it later.
out=$(git -c color.status=always status) printf "$out\n"
For a more general solution that works with other programs that do not provide color redefinition, the best way to do this is with a script, as shown in Can the color output be captured via shell redirection?
In such cases, you would like to use status=$(script -q /dev/null git status | cat)
vmrob source share