Bash:带有变量的彩色输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10466749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:01:32  来源:igfitidea点击:

Bash: Colored Output with a Variable

bashvariables

提问by derp

I have the following function:

我有以下功能:

function pause #for prompted pause until ENTER
{


prompt=""
    echo -e -n "\E[36m" #color output text cyan
    echo -e -n '\E[0m' #ends colored output
    read -p "$*"  #read keys from user until ENTER.
    clear

}

pause "Press enter to continue..."

However, my function refuses to apply the cyan color to the string I pass into the function.

但是,我的函数拒绝将青色应用于传递给函数的字符串。

A similar question was asked here, but it seems that I'm doing everything correctly...

这里有人问一个类似的问题,但似乎我做的一切都正确......

采纳答案by marcelog

I've slightly changed your code:

我稍微改变了你的代码:

#!/bin/bash

function pause() {
    prompt=""
    echo -e -n "3[1;36m$prompt"
    echo -e -n '3[0m'
    read
    clear
}

pause "Press enter to continue..."

What I've changed:

我改变了什么:

  1. You were initializing prompt to $3, when the correct argument was $1
  2. The ANSI sequence was incorrect. See: http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
  3. The call to read was incorrect, you were passing several arguments do to the use of $*. In this particular case you are discarding the input, so it's not even necessary to save the result of read. I suggest you to read the manpage: http://linux.die.net/man/1/bashto see how to exactly use read. If you pass in several arguments, those arguments will be mapped to variable names that will contain the different fields inputted in the line.
  1. 当正确的参数为 $1 时,您将提示初始化为 $3
  2. ANSI 序列不正确。请参阅:http: //tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
  3. 对 read 的调用不正确,您传递了几个参数 do 来使用 $*。在这种特殊情况下,您将丢弃输入,因此甚至不需要保存读取结果。我建议您阅读手册页:http: //linux.die.net/man/1/bash以了解如何准确使用 read。如果您传入多个参数,这些参数将被映射到变量名称,这些名称将包含在该行中输入的不同字段。

回答by dschulz

Try this:

尝试这个:

RESTORE='3[0m'

RED='3[00;31m'
GREEN='3[00;32m'
YELLOW='3[00;33m'
BLUE='3[00;34m'
PURPLE='3[00;35m'
CYAN='3[00;36m'
LIGHTGRAY='3[00;37m'

LRED='3[01;31m'
LGREEN='3[01;32m'
LYELLOW='3[01;33m'
LBLUE='3[01;34m'
LPURPLE='3[01;35m'
LCYAN='3[01;36m'
WHITE='3[01;37m'

function test_colors(){

  echo -e "${GREEN}Hello ${CYAN}THERE${RESTORE} Restored here ${LCYAN}HELLO again ${RED} Red socks aren't sexy ${BLUE} neither are blue ${RESTORE} "

}

function pause(){
  echo -en "${CYAN}"
  read -p "[Paused]  $*" FOO_discarded
  echo -en "${RESTORE}"
}


test_colors
pause "Hit any key to continue"

And there's more fun with backgrounds

背景更有趣

echo -e "3[01;41;35mTRY THIS3[0m"
echo -e "3[02;44;35mAND THIS3[0m"
echo -e "3[03;42;31mAND THIS3[0m"
echo -e "3[04;44;33mAND THIS3[0m"
echo -e "3[05;44;33mAND THIS3[0m"

回答by user4487642

To save others time:

为了节省其他人的时间:

https://gist.github.com/elucify/c7ccfee9f13b42f11f81

https://gist.github.com/elucify/c7ccfee9f13b42f11f81

No need to $(echo -ne)all over the place, because the variables defined in the gist above already contain the control characters. The leading/trailing \001& \002tell bash that the control characters shouldn't take up space, otherwise using these in a $PS1will confuse readline.

不需要$(echo -ne)到处都是,因为上面gist中定义的变量已经包含了控制字符。前导/尾随\001&\002告诉 bash 控制字符不应占用空间,否则在 a 中使用这些$PS1会混淆readline.

RESTORE=$(echo -en '
echo -e -n "\E[36m" #color output text cyan
13[0m
echo -e -n "\E[36m" #color output text cyan
2') RED=$(echo -en '
prompt=""
13[00;31m
function pause #for prompted pause until ENTER
{
    read -p $'\E[36m'"$*"$'\E[0m'  #read keys from user until ENTER.
    clear
}

pause "Press enter to continue..."
2') GREEN=$(echo -en '
cyan=$'\E[36m'
reset=$'\E[0m'
read -p "$cyan$*$reset"
13[00;32m##代码##2') YELLOW=$(echo -en '##代码##13[00;33m##代码##2') BLUE=$(echo -en '##代码##13[00;34m##代码##2') MAGENTA=$(echo -en '##代码##13[00;35m##代码##2') PURPLE=$(echo -en '##代码##13[00;35m##代码##2') CYAN=$(echo -en '##代码##13[00;36m##代码##2') LIGHTGRAY=$(echo -en '##代码##13[00;37m##代码##2') LRED=$(echo -en '##代码##13[01;31m##代码##2') LGREEN=$(echo -en '##代码##13[01;32m##代码##2') LYELLOW=$(echo -en '##代码##13[01;33m##代码##2') LBLUE=$(echo -en '##代码##13[01;34m##代码##2') LMAGENTA=$(echo -en '##代码##13[01;35m##代码##2') LPURPLE=$(echo -en '##代码##13[01;35m##代码##2') LCYAN=$(echo -en '##代码##13[01;36m##代码##2') WHITE=$(echo -en '##代码##13[01;37m##代码##2') # Test echo ${RED}RED${GREEN}GREEN${YELLOW}YELLOW${BLUE}BLUE${PURPLE}PURPLE${CYAN}CYAN${WHITE}WHITE${RESTORE}

回答by Paused until further notice.

The problem is that this line:

问题是这一行:

##代码##

should be:

应该:

##代码##

and you should eliminate this line since you're not using the variable:

并且您应该消除这一行,因为您没有使用该变量:

##代码##

Also the end sequence should be moved into the readprompt. In fact, the begin sequence can be, too.

此外,应将结束序列移至read提示中。事实上,开始序列也可以。

The result:

结果:

##代码##

The colors could be put into variables:

颜色可以放入变量中:

##代码##

The $''causes the escape sequence to be interpreted just like echo -e.

$''使转义序列被解释一样echo -e