I've started utilizing ANSI color codes in batch scripts for Windows 10. Today I discovered that suddenly colors stopped working, and I traced it to a for
command with an embedded MSYS command. For example:
@echo off
echo ^[[92mThis works.^[[0m
for %%f in (x) do @echo ^[[93mThis also works.^[[0m
for /l %%f in (1,1,1) do @echo ^[[94mThis too works.^[[0m
for /f "tokens=*" %%f in ('date /t') do @echo ^[[95mThis one works.^[[0m
for /f "tokens=*" %%f in ('sha1sum.exe ^< NUL') do @echo ^[[91mThis one DOES NOT work.^[[0m
echo ^[[92mThis works.^[[0m
(Note: Where you see "^[" needs to be a single ESC character, ASCII 27, instead. E.g., type <C-V><ESC>
in Vim's insert mode.)
In my case, sha1sum.exe refers to C:\Program Files\Git\usr\bin\sha1sum.exe
from Git for Windows. We all know batch scripts/cmd.exe have their own wizardry about them, but I can't think of an explanation for this. Is it somehow toggling color interpretation off for the duration of the command line? And if so would there be a way to force it back on?
APPEND:
It's worth noting that it seems to affect the entire "command line", meaning if you have nested ifs and fors using parenthesis the entire bit wrapped in parenthesis appears to be affected.