Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I changed my bash prompt to this:

PS1="\[\033[1;31m[\[\033[1;33m\$(date +%H:%M)\[\033[1;31m] \u:\[\033[1;32m\W\[\033[1;37m\$\[\033[0m\] "

Sorry for the long line, it is mostly due to colors. Basically when I hit the up arrow to go back in Bash history the prompt disappears and everything screws up. It is similar to this question but I don't know if a \[ is missing and where.

share|improve this question
add comment

3 Answers 3

up vote 8 down vote accepted

Let's clean it up a bit and make it more portable. In general, it's best to use tput to generate the control sequences instead of hard coding them, as described in Bash FAQ 53. This way it is much easier to find your missing \].

red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
white=$(tput setaf 7)
reset=$(tput sgr0)
PS1="\[$red\][\[$yellow\]\A\[$red\]] \u:\[$green\]\W\[$white\]\$\[$reset\] "

Note that I replaced your date invocation with the built-in prompt escape \A that displays the same thing (24-hour time in HH:MM format).

There is one caveat with this approach as described in the linked Bash FAQ, where the output would be garbled if any of the tput control sequences output something that happened to contain a prompt escape. I have never run into that issue so I generally ignore it.

share|improve this answer
1  
Thank you! I didn't know of tput. This way everything is more clear and readable. Thanks! –  rubik Aug 14 '12 at 18:06
1  
Wow it works very well. I just added bold_style=$(tput bold) and \[$bold_style\] at the start of the string to make everything bold like in my question. –  rubik Aug 14 '12 at 18:13
add comment

You have unbalanced your \[ and \]'s. Control sequences need to be wrapped in them, so that bash knows they don't actually occupy any space in your terminal.

Try this:

PS1="\[\033[1;31m\][\[\033[1;33m\]\$(date +%H:%M)\[\033[1;31m\]] \u:\[\033[1;32m\]\W\[\033[1;37m\]\$\[\033[0m\] "

@jw013's solution is even better, though, as it's much cleaner.

share|improve this answer
    
Oh I hadn't understood that every control sequence needed to be wrapped like that. Thanks! –  rubik Aug 14 '12 at 18:04
add comment

The ASCII table has a reference of colors, bold, italic, and etc.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.