Search code examples
bashshellvagrantvagrant-provision

How to output error in custom color in Vagrant provision script?


I want my Vagrant provision script to run some checks that will require user action if they're not satisfied. As easy as:

if [ ! -f /some/required/file ]; then
    echo "[Error] Please do required stuff before provisioning"
    exit
fi

But, as long as this is not a real error, I got the echo printed in green. I'd like my output to be red (or, a different color at least) to alert the user.

I tried:

echo "\033[31m[Error] Blah blah blah"

that works locally, but on Vagrant output the color code gets escaped and I got it echoed in green instead.

Is that possible?


Solution

  • This is happening because some tools write some of their messages to stderr, which Vagrant then interprets as an error and prints in red.

    Not all terminals support ANSI colour codes and Vagrant don't take care of that. Said that, I won't suggest colorizing a word by sending it to stderr unless it is an error.

    To achieve that you can simply:

    echo "Your error message" > /dev/stderr