LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Bash prompt. It looks fine initially, but there seems to be a hidden error. (https://www.linuxquestions.org/questions/linux-general-1/bash-prompt-it-looks-fine-initially-but-there-seems-to-be-a-hidden-error-883993/)

WyattOil 06-01-2011 02:22 PM

Bash prompt. It looks fine initially, but there seems to be a hidden error.
 
I've written custom prompts for several boxes but this one has an error I just can't identify and need a second set of eyes to help solve it.


I've set the following as my prompt:
PS1="\[\e[30;42m\]\u@\h\[\e[0m\]\[\e[30;47m\] \#\]\[\e[0m\]\[\e[32;1;40m\]\w>\[\e[0m\] "
(hostname/un obscured & image enlarged slightly to make it easier to read.)

Everything looks fine initially as you can see here:
http://www.upl.co/uploads/prompt1-45.png
1- It sets the username@host in back on green text.
2- It then changes to an off-white an prints the command number for the terminal.
3- Next, changes to a green on black font and prints the working directory.
4- Finally is prints a ">" character and a space.


The problem occurs when I try to "up arrow" to reuse and/or edit a prior command. It prints the prior command fine, but if I arrow over to edit the command sometimes the first character can not be deleted from displaying as you can see in the following composite screen-shot

http://www.upl.co/uploads/prompt7.jpg
Here I did a simple ps and piped it through grep. After getting the output, I up-arrow to repeat the command. As you can see by the second section the cursor only goes back to the "s" in ps. The "p" can not be deleted. Hitting enter just displays a new line, so the "p" was just a ghost being displayed and not really there.

Can anyone else see why that randomly occurs? I must have a goof in the prompt, but I sure as heck can't see it.

Thanks in advance.

MTK358 06-01-2011 03:17 PM

Your original prompt had an unbalanced "\]". Also, you don't need to put "\[\]" around every control code (i.e. you did "\[<control code>\]\[<control code\]" when "\[<control code><control code\]" would do). You put them around text that doesn't move the cursor forward, so that bash knows where to position the command's test when using up-arrow history. Finally, you don't need to reset the color before changing it again.

Try this:

Code:

PS1="\[\e[30;42m\]\u@\h\[\e[30;47m\] \#\[\e[32;1;40m\]\w>\[\e[0m\] "

WyattOil 06-01-2011 06:55 PM

Quote:

Originally Posted by MTK358 (Post 4373434)
Your original prompt...

Wow, thanks so much.

Marking this solved :)


All times are GMT -5. The time now is 02:59 AM.