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 outpu
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
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.
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
I have another solution that allows you to use variables to change the text's format. I echo -e
the the output I want into the -p
argument of the read
command.
Here's an example:
RESET="\033[0m"
BOLD="\033[1m"
YELLOW="\033[38;5;11m"
read -p "$(echo -e $BOLD$YELLOW"foo bar "$RESET)" INPUT_VARIABLE