Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this bash prompt acting strangely/disappearing, and how do I fix it (OS X)?

I admit that I use a somewhat long-winded bash prompt:

--(username)-(Wed April 01|12:00:00)--(~ $

Recently, I got the bright idea to change it so that depending on the exit value from the previous command, if success, the interior elements of the ()'s would be green, and if failure, they would be red. I got it working for the most part (some odd exit statuses will change the color to something else, but I'm ok with it), but when typing a command which is more than one line, and causes the terminal to scroll, the prompt disappears! My prompt worked fine when there was no color, so I'm guessing it is related to my color escaping, and particularly my unclosed ['s, but I can't pin it down.

#.profile
export PS1='--(\e[$((32-${?}))m\u\e[0m)-(\e[$((32-${?}))m\d\e[0m|\e[$((32-${?}))m\T\e[0m)--(\e[$((32-${?}))m\w\e[0m \$ '

Thanks in advance!

like image 741
David Smith Avatar asked Apr 01 '09 18:04

David Smith


People also ask

How do I get the command prompt back on Mac?

Type "cd -" in the Terminal window and press "Return." The Terminal returns to the previous directory.

How do I permanently change bash prompt?

After you have experimented with text customization and colorization of your prompt, and reached a final that you want to set permanently for all your bash sessions, you need to edit your bashrc file. Save the file by pressing Ctrl+X and then by pressing Y. The changes to your bash prompt will now be permanent.

What does the Bash prompt mean?

The Bash prompt is set by the environment variable PS1 (Prompt String 1), which is used for interactive shell prompts. There is also a PS2 variable, which is used when more input is required to complete a Bash command.


1 Answers

It sounds like this should solve your problem.

This seems to work for me*:

export PS1='--(\[\e[$((32-${?}))m\]\u\[\e[0m\])-(\[\e[$((32-${?}))m\]\d\[\e[0m\]|\[\e[$((32-${?}))m\]\T\[\e[0m\])--(\[\e[$((32-${?}))m\]\w\[\e[0m\] \$ '

* well, really export PS1='\u@\h:\w\$ ' works for me

To quote the linked post, the answer lies in adding \[ and \] around all of your color sequences in your PS1 declaration:

Before I had the following value for PS1:

'\e[0;34m\h:\w [!]\$\e[0m '

which gave me a nice blue prompt of the following form

hostname:working-directory [command-number]$

However, I had the same line-wrapping problem you did. The fix was to insert \[ and \] around the ANSI escapes so that the shell knows not to include them in the line wrapping calculation. This results in the following value for PS1:

'\[\e[0;34m\]\h:\w [!]\$\[\e[m\] '

like image 150
Chas. Owens Avatar answered Sep 27 '22 23:09

Chas. Owens