How to echo with different colors in the Windows command line

前端 未结 23 1488
野性不改
野性不改 2020-11-22 08:55

I know that the color bf command sets the colors of the whole command line window but I wanted to to print one single line in a different color.

23条回答
  •  醉梦人生
    2020-11-22 09:47

    call :color_echo "blue" "blue txt"
    call :color_echo "red" "red txt"
    echo "white txt"
    
    
    REM : https://www.robvanderwoude.com/ansi.php
    :color_echo
        @echo off
    
        set "color=%~1"
        set "txt=%~2"
    
        set ESC=
        set black=%ESC%[30m
        set red=%ESC%[31m
        set green=%ESC%[32m
        set yellow=%ESC%[33m
        set blue=%ESC%[34m
        set magenta=%ESC%[35m
        set cyan=%ESC%[36m
        set white=%ESC%[37m
    
        if "%~1" == "black"   set "color=!black!"
        if "%~1" == "red"     set "color=!red!"
        if "%~1" == "green"   set "color=!green!"
        if "%~1" == "yellow"  set "color=!yellow!"
        if "%~1" == "blue"    set "color=!blue!"
        if "%~1" == "magenta" set "color=!magenta!"
        if "%~1" == "cyan"    set "color=!cyan!"
        if "%~1" == "white"   set "color=!white!"
    
        echo | set /p="!color!!txt!"
        echo.
    
        REM : return to standard white color
        echo | set /p="!white!"
    
        REM : exiting the function only
        EXIT /B 0
    

提交回复
热议问题