Read Command : Display the prompt in color (or enable interpretation of backslash escapes)

I often use something like read -e -p "> All good ? (y/n)" -n 1 confirm; to ask a confirm to the user.

I'm looking for a way to colorize the output, as the command echo -e does :

echo -e "\033[31m";
echo "Foobar";       // will be displayed in red
echo -e "\033[00m";

I'm using xterm.

In man echo, it says :

-e enable interpretation of backslash escapes

Is there a way to do the same thing with the read command ? (nothing in the man page :( -r option doesn't work)


  • read won't process any special escapes in the argument to -p, so you need to specify them literally. bash's ANSI-quoted strings are useful for this:

    read -p $'\e[31mFoobar\e[0m: ' foo

    You should also be able to type a literal escape character with Control-v Escape, which will show up as ^[ in the terminal:

    read -p '^[[31mFoobar^[[0m: ' foo