linuxbashterminal-color

How to make bash command echo lines green


I have a bash script with command echo turned on (set -x). I want the echo lines to be printed in green text, but the output of the actual commands to be in whatever the default color is. I can use $PS4 to get the green, but I don't see a way to turn it of after each echo, so the result is that all output becomes green.


Solution

  • Try this:

    #!/bin/bash
    exec 3<> >(sed -e "s/.*/\o033[0;32m&\o033[0m/")
    BASH_XTRACEFD=3
    set -x
    ...
    

    (requires bash 4.1, I believe; use awk or perl or bash builtins instead of sed if you prefer)