You are not logged in.

#1 2008-10-12 13:36:01

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,001
Website

[Solved] buggy PS1 in bash

Hi, what I'm trying to achieve is a bash prompt that is pretty default (user@host current-directory) but, the current directory must be shown in red if the last command was unsucessful, green otherwise.

This is in my .bashrc

if [ $(tput colors) -gt 0 ] ; then
    RED=$(tput setaf 1)
    GREEN=$(tput setaf 2)
    RST=$(tput op)
fi

bash_prompt_command() {
        last_exit=$?
        exit_to_color=$(exit_to_color $last_exit)
}
export PROMPT_COMMAND=bash_prompt_command

exit_to_color() {
    [ $1 == 0 ] && echo $GREEN || echo $RED
}

PS1="\u@\h \$exit_to_color\W\[$RST\] \$"

The color thingie works, but sometimes \W doesn't show anything at all. example:

dieter@dieter-mbp ~ $pwd
/home/dieter
dieter@dieter-mbp ~ $cd .reference/
dieter@dieter-mbp .reference $cd
dieter@dieter-mbp ~ $cd workspaces/
dieter@dieter-mbp  $pwd
/home/dieter/workspaces
dieter@dieter-mbp  $

Also, sometimes I see 'leftovers' from the previous command on my prompt, if I use bash history functions and I can't erase them.

dieter@dieter-mbp ~ $svn diff workspaces/eclipse/tools/scripts/cdnbench.php  >/dev/null
dieter@dieter-mbp ~ $svn d    #<--- what I did here is press up arrow (shows previous command perfectly), and then down arrow (doesn't clean up everything)

Note that it's just a visual artifact, not a functional one:

dieter@dieter-mbp ~ $svn diff workspaces/eclipse/tools/scripts/cdnbench.php  >/dev/null
dieter@dieter-mbp ~ $svn decho ok
ok
dieter@dieter-mbp ~ $  # if i press up arrow at this point, it shows the previous command 'echo ok', pressing down works fine too

Any ideas?

Last edited by Dieter@be (2008-10-13 18:36:44)


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#2 2008-10-12 15:11:16

pointone
Wiki Admin
From: Waterloo, ON
Registered: 2008-02-21
Posts: 379

Re: [Solved] buggy PS1 in bash

Try disabling the colour effects and see if that clears up the problem. It may be interfering with your prompt.


M*cr*s*ft: Who needs quality when you have marketing?

Offline

#3 2008-10-12 15:26:28

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,001
Website

Re: [Solved] buggy PS1 in bash

Well, when I do that (I change the PROMPT_COMMAND to 'true' instead of 'bash_prompt_command'), then the 'leftover from previous command' artifact is gone.  However the '\W' sometimes empty is exactly the same.


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#4 2008-10-13 03:02:30

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: [Solved] buggy PS1 in bash

You need to escape color codes with \[ \] or it will count them as printing characters, hence the visual offset.

Offline

#5 2008-10-13 18:36:19

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,001
Website

Re: [Solved] buggy PS1 in bash

Daenyth wrote:

You need to escape color codes with \[ \] or it will count them as printing characters, hence the visual offset.

Thanks a lot !

Changed it to

PS1="\u@\h \[\$exit_to_color\]\W\[$RST\] \$"

works like a charm now cool


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

Board footer

Powered by FluxBB