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?
To store the output of a command in a variable, you can use the shell command substitution feature in the forms below: variable_name=$(command) variable_name=$(command [option ...] arg1 arg2 ...) OR variable_name='command' variable_name='command [option ...]
The problem is not that bash strips the color output before storing the text, but that git
refuses 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. --color
for ls
). Consult your git
documentation whether it also has such an override option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With