0

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"
will
  • 10,260
  • 6
  • 46
  • 69

3 Answers3

1

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

sehe
  • 374,641
  • 47
  • 450
  • 633
  • updated with more hints; see other good hints http://stackoverflow.com/questions/4557534/how-can-i-intermittently-show-my-history-command-number-in-my-shell-prompt – sehe Apr 04 '11 at 10:58
  • I ended up going with something along these lines, realised that ${a} wasn't being changed each time or something. – will Apr 04 '11 at 12:02
1

Instead of double quotes in PS1="\e[0;${a}m$ \e[m" use single quotes, like this:

PS1='\e[0;${a}m$ \e[m'

... so that ${a} will be evaluated each time.

Mark Longair
  • 446,582
  • 72
  • 411
  • 327
0

Basically PROMPT_COMMAND is the Bash feature you are probably looking for.

From man bash(1):

PROMPT_COMMAND

If set, the value is executed as a command prior to issuing each primary prompt.

So:

function rotate_prompt_colour() {
    ROTATE_COLOUR=$(( (ROTATE_COLOUR + 1) % 7))
    PS1="\h : \w \[\e[$(( 30 + ROTATE_COLOUR ))m\]\$\[\e[0m\] "
}

export PROMPT_COMMAND=rotate_prompt_colour

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.

Community
  • 1
  • 1
Chen Levy
  • 15,438
  • 17
  • 74
  • 92