Question

I recently found a post that gave a solution to have the text output by stderr a different color for Linux (bash)

They created the following bash script script

#!/bin/bash
{ $* 2>&1>&3|sed 's,.*,\x1B[33m&\x1B[0m,'>&2;} 3>&1

this causes the output to print yellow text when it is from stderr. stdout still prints the same color.

the script was saved in a directory on $PATH called color. This allows me to run the script with make or scons and it will hi-lite all of the text from stderr in yellow. (could make the text red by changing 33m to 31m)

color make CPU=x64 

This is quite useful for finding errors when compiling.

Is there a similar script that could be used for Windows cmd shell?

Note: I have sed installed on my windows computer if that is helpful.

Was it helpful?

Solution

As for support for ANSI escape codes under Windows' cmd.exe, see ansicon. After translating your redirection logic to cmd.exe syntax I prepared the following color.bat file:

@Echo Off
(((%* 1>&3) 2>&1) | "c:\Program Files (x86)\GnuWin32\bin\sed.exe" "s,.*,\x1B[33m&\x1B[0m," 1>&2) 3>&1

Unfortunately the streams get mixed (on some lines the characters from stdout and stderr are mixed together in single line). Maybe this behavior depends on version of sed.exe used, so give it a try.

If this does not work, consider using a minimum cygwin install. I tested your color.sh script and I was able to launch a .bat file and it worked correctly without mixing streams. The syntax I used was:

./color.sh cmd /c test.bat

OTHER TIPS

I devised a method to get an equivalent solution using pure Batch commands, i.e. no Ansi, no sed.exe, etc., just findstr is used:

any_command 2>&1 1>&3 | findstr /N /A:4E "^"

In this case the different color is not given to the whole lines from stderr, but just to a line number provided by findstr that, however, should be enough for the stated requirements. I will write a small auxiliary .exe program soon that will show entire stderr lines in another color.

@MBu, @gnash117

I renamed color to co since COLOR is a Windows command and I extended it to:

:: color output - Displays stderr output in different color
::
:: Credits to MBu and gnash117 at http://stackoverflow.com/questions/10095886/change-text-output-color-on-windows-for-stderr#10118710
:: 
:: Requires:
::   - http://sourceforge.net/projects/mingw/files/MSYS/
::   - http://adoxa.altervista.org/ansicon/
::   - http://www.autohotkey.com/
::
@echo off

if "%1"=="" goto :Help

:: 1;31 ... intense red foreground (see https://github.com/adoxa/ansicon/blob/master/sequences.txt for more colors)
:: \x07 ... BEL, but doesn't work :-(
(((%* 1>&3) 2>&1) | sed "s/.*/\x07\x1B[1;31m&\x1B[0m/" 1>&2) 3>&1
goto :EOF

:Help
setlocal
::------------------------------------------------
:: Adapt these in pairs according to your likings
set invokeKeys=[Shift]+[Enter]
set invokeHotkeys=+Enter
set invokeAfterRemoveKeys=[Alt]+[Enter]
set invokeAfterRemoveHotkeys=!Enter
set removeKeys=[Alt]+[c]
set removeHotkeys=!c
::-----------------------------------------------
set invokeText=invokes the entered command after preceding it with '%0 '
set invokeAfterRemoveText=invokes the entered command after removing the first three characters from the beginning
set removeText=removes the first three characters from the command line
echo Colors a command's stderr output as defined in %~f0
echo Usage:
echo   - Preceed a command with '%0 ' (e.g.: 'co dir not.existing.file' will show the resulting error message in a different color)
echo   - If the AutoHotkey script below is active:
echo     - %invokeKeys% ... %invokeText%
echo     - %invokeAfterRemoveKeys% ... %invokeAfterRemoveText%
echo     - %removeKeys% ... %removeText%
echo(
echo       The latter two are useful when using the command line history and having used %invokeKeys% before.
echo(
echo     Enabled by AutoHotkey script:
echo(
echo       #IfWinActive ahk_class ConsoleWindowClass
echo       ; %invokeText%
echo       %invokeHotkeys%::Send {Home}%0 {Enter}
echo       ; %invokeAfterRemoveText%
echo       %invokeAfterRemoveHotkeys%::SendInput {Home}{Del 3}{Enter}
echo       ; %removeText%
echo       %removeHotkeys%::SendInput {Home}{Del 3}{End}
echo       #IfWinActive
endlocal
:EOF
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top