如何使终端中 $ 位的颜色每行改变颜色?
目前我有这个:
function xtitle2() # Adds some text in the terminal frame.
{
export var1=`echo $HOSTNAME | perl -pe 's/^([a-zA-Z0-9]+)\.(.*)$/\1/g'`
export var2=`pwd`
echo -n -e "\033]0;$var1 : $var2\007"
a=$(( $a + 1 ))
if (( $a > 36 ))
then
a=30
fi
}
PROMPT_COMMAND="xtitle2"
PS1="\e[0;${a}m$ \e[m"
但它只在我这样做时改变颜色
$. ~/.profile
,但我希望它在每次输入任何命令时改变颜色......
我该怎么做?
EIDT:
最终是这样的:
function xtitle2() # Adds some text in the terminal frame.
{
export var1=`echo $HOSTNAME | perl -pe 's/^([a-zA-Z0-9]+)\.(.*)$/\1/g'`
export var2=`pwd`
echo -n -e "\033]0;$var1 : $var2\007"
if [ -z $a ]
then
a=29
fi
a=$(( $a + 1 ))
if (( $a > 36 ))
then
a=30
fi
PS1="\[\033[${a}m\]$\[\e[0m\]"
}
export PROMPT_COMMAND="xtitle2"
currently i have this:
function xtitle2() # Adds some text in the terminal frame.
{
export var1=`echo $HOSTNAME | perl -pe 's/^([a-zA-Z0-9]+)\.(.*)$/\1/g'`
export var2=`pwd`
echo -n -e "\033]0;$var1 : $var2\007"
a=$(( $a + 1 ))
if (( $a > 36 ))
then
a=30
fi
}
PROMPT_COMMAND="xtitle2"
PS1="\e[0;${a}m$ \e[m"
but it only changes the colour when i do
$. ~/.profile
but i want it to change the colour every time any command is entered...
how do i do this?
EIDT:
ended up going with this:
function xtitle2() # Adds some text in the terminal frame.
{
export var1=`echo $HOSTNAME | perl -pe 's/^([a-zA-Z0-9]+)\.(.*)$/\1/g'`
export var2=`pwd`
echo -n -e "\033]0;$var1 : $var2\007"
if [ -z $a ]
then
a=29
fi
a=$(( $a + 1 ))
if (( $a > 36 ))
then
a=30
fi
PS1="\[\033[${a}m\]$\[\e[0m\]"
}
export PROMPT_COMMAND="xtitle2"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在你的 PS1 设置中包含“$(xtitle2)”
当然你需要重构一下 xtitle2;好消息是您不必再为此目的滥用 PROMPT_COMMAND。此外,除了 a 之外的所有变量都可以是本地变量。
您将需要使用
$(($HISTCMD % 30))
而不是变量 a 的混乱Include "$(xtitle2)" in your PS1 setting
Of course you need to refactor xtitle2 a bit; the good news is that you won't have to abuse PROMPT_COMMAND for this purpose anymore. Also, all the vars except a could be local.
You will want to use
$(($HISTCMD % 30))
instead of the jumble with variable a在
PS1="\e[0;${a}m$ \e[m"
中使用单引号代替双引号,如下所示:... 这样
${a}
每次都会被评估。Instead of double quotes in
PS1="\e[0;${a}m$ \e[m"
use single quotes, like this:... so that
${a}
will be evaluated each time.基本上
PROMPT_COMMAND
是您可能正在寻找的 Bash 功能。来自
man bash(1)
:所以:
请注意,
PS1
有一些有用的转义序列,可以为您省去一些麻烦。另请注意 ANSI 序列周围的\[...\]
以避免一些 readline 奇怪的情况。Basically
PROMPT_COMMAND
is the Bash feature you are probably looking for.From
man bash(1)
:So:
Note that
PS1
has some useful escape sequences that can save you some hassle. Also note the\[...\]
around ANSI sequences to avoid some readline weirdness.