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
Here's another solution that allows to use variables to change the text's format. echo -e the wanted output into the -p argument of the read command.
Example:
RESET="\033[0m"
BOLD="\033[1m"
YELLOW="\033[38;5;11m"
read -p "$(echo -e $BOLD$YELLOW"foo bar "$RESET)" INPUT_VARIABLE
Break your query into two components:
e.g:
echo -e -n "\e[0;31mAll good (y/n)? " # Display prompt in red
echo -e -n '\e[0;0m' # Turn off coloured output
read # Collect the user input
The echo -n option suppresses the trailing newline.
this work for me :
BC=$'\e[4m'
EC=$'\e[0m'
while true; do
read -p "Do you wish to copy table from ${BC}$HOST $PORT${EC} to ${BC}$LOCAL_HOST $LOCAL_PORT${EC}? (y or n)" yn
case $yn in
....
done
Results are as follows:

more example ,see the show case ,link is :
mysqlis
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With