bash - 读取命令 : Display the prompt in color (or enable interpretation of backslash escapes)

标签 bash unix terminal

我经常使用像 read -e -p "> All good ? (y/n)"-n 1 confirm; 这样的东西来向用户询问确认。

我正在寻找一种方法来为输出着色,就像命令 echo -e 所做的那样:

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

我正在使用 xterm。

man echo 中,它说:

-e enable interpretation of backslash escapes

有没有办法用 read 命令做同样的事情? (手册页中没有任何内容 :( -r 选项不起作用)

最佳答案

read 不会处理 -p 参数中的任何特殊转义符,因此您需要按字面意思指定它们。 bash 的 ANSI 引号字符串对此很有用:

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

您还应该能够使用 Control-v Escape 键入文字转义字符,这将显示为 ^ [ 在终端中:

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

关于bash - 读取命令 : Display the prompt in color (or enable interpretation of backslash escapes),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24998434/

相关文章:

unix - *nix 语言环境设置中 zh_CN.UTF-8 和 en_US.UTF-8 有什么不同?

c - (unix/C) "stty: stdin isn' t a terminal"when using system() function

wordpress - 使用SSH连接到服务器上的Git

bash - Shell 脚本未通过 crontab 运行,但手动运行良好

linux - 当我在 sed 中设置 100.000 距离时,使用命令 sed 和 "Argument list too long"第一次压缩时出现 Tar 错误

C代码: Calling function in main

python - 汇编的十六进制内存地址

c++ - qt GUI 应用程序内的终端

bash - 按参数过滤的总计数 - bash

bash - find -print0 和 xargs -0 开关是否有等效的 grep?