简体   繁体   English

在“read -p”命令中为变量着色

[英]Coloring a variable in "read -p" command

I would like to color a variable in the "read -p" command.我想在“read -p”命令中为变量着色。 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.但是,如果我放置一个变量而不是“Foobar”,则不会显示该值,因为单引号会阻止变量 $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

You can terminate and restart the escape sequence, thus moving the variable expansion outside, as per the following transcript:您可以终止并重新启动转义序列,从而将变量扩展移到外面,按照以下记录:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM