Home > Net >  Coloring a variable in "read -p" command
Coloring a variable in "read -p" command

Time:02-01

I would like to color a variable in the "read -p" command. In another topic i found a way to color the text string like this:

read -p $'\e[31mFoobar\e[0m: ' <= works

But if I put a variable instead of 'Foobar' the value is not shown because the single quotes are preventing the call of the variable $mmd.

read -p $'\e[31m $mmd \e[0m: ' <= doesnt ork

Do you know a way to make this work?

I tried:

read -p $'\e[31m $mmd \e[0m: ' <= doesnt work

read -p $"\e[31m $mmd \e[0m: " <= doesnt work

CodePudding user response:

You can terminate and restart the escape sequence, thus moving the variable expansion outside, as per the following transcript:

pax:~> export mmd="Enter something"
pax:~> read -p $'\e[31m'"${mmd}"$'\e[0m: '
Enter something: hello<enter>
pax:~> echo ${REPLY}
hello
  • Related