bash wrapper to color stderr red

后端 未结 2 805
谎友^
谎友^ 2021-01-20 22:48

Bash supports colors, i.e. \\033[31m switches to red and \\033[0m switches back to uncolored.

I would like to make a small bash-wrapper that re

2条回答
  •  孤独总比滥情好
    2021-01-20 23:34

    Based on andrewdotn's wrapper

    Changes:

    • Puts the stderr output back to stderr
    • Avoid echo -e processing content in the lines

    wrapper

    #!/bin/bash
    
    "${@}" 2> >(
    while read line; do
        echo -ne "\033[31m" 1>&2
        echo -n "${line}" 1>&2
        echo -e "\033[0m" 1>&2
    done
    )
    

    Issues: The output lines end up grouped, rather than mixed stdout/stderr

    Test script:

    #!/bin/bash
    
    echo Hi
    echo "\033[32mStuff"
    echo message
    echo error 1>&2
    echo message
    echo error 1>&2
    echo message
    echo error 1>&2
    

    Output:

    Hi
    \033[32mStuff
    message
    message
    message
    error # <- shows up red
    error # <- shows up red
    error # <- shows up red
    

提交回复
热议问题