LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   PS1 bash prompt with new line and color (https://www.linuxquestions.org/questions/linux-newbie-8/ps1-bash-prompt-with-new-line-and-color-4175600041/)

wolfv 02-17-2017 10:26 PM

PS1 bash prompt with new line and color
 
I am trying to create a bash prompt with new line and color.
The first three prompts behave as expected:
Code:

PS1="[]\$ "                  # 1) simple prompt: []$
PS1="[]\n\$ "                # 2) prompt with new line
PS1="\e[1;33m[]\$\e[m "      # 3) prompt with color
PS1="\e[1;33m[]\n\$\e[m "    # 4) prompt with new line and color fails

The fourth prompt fails. The prompt history does not displaying correctly when a command is more than 6 chars long:
Code:

$ export PS1="\e[1;33m[]\n\$\e[m "
[]
$ echo xx
xx
[]
$ ech

The last line is output after arrow up then arrow down. It should be blank, but it displays characters from the previous command.
What wrong with the exported prompt?

Thank you.

goumba 02-17-2017 10:29 PM

Bash can be funny with escape codes.

Try the suggestion here, http://stackoverflow.com/questions/7...ompt-correctly, see if that helps.

wolfv 02-17-2017 11:28 PM

Quote:

Originally Posted by goumba (Post 5672445)
Bash can be funny with escape codes.

Try the suggestion here, http://stackoverflow.com/questions/7...ompt-correctly, see if that helps.

Thanks for the quick response.
The link is about the \[...\] sequence.
My problem prompt "\e[1;33m[]\n\$\e[m " contains neither \[ nor \].

wolfv 02-18-2017 01:26 AM

This works:
Code:

export PS1="\e[1;33m[]\e[m\n\$ "
The '$' is not colored, but that's OK.

grail 02-18-2017 02:49 AM

Unless you are expanding any variables inside (which can be problematic and a pain) I would suggest using single quotes so as to not have anything interpreted prior to being assigned.

Habitual 02-18-2017 06:12 AM

http://bashrcgenerator.com/

goumba 02-18-2017 06:44 AM

Quote:

Originally Posted by wolfv (Post 5672455)
Thanks for the quick response.
The link is about the \[...\] sequence.
My problem prompt "\e[1;33m[]\n\$\e[m " contains neither \[ nor \].

No, yours does not. As the link explains, it may or may not help with your problem. The line length can get all screwy with escape codes, and enclosing the non printing (in this case the color) codes in the escaped brackets can help with the problem. It did so for me with my colored prompt.

Shadow_7 02-18-2017 06:51 AM

Try \033 instead of \e. What I had to do for a bash script that does color. It's the octal value for e.

wolfv 02-18-2017 10:18 AM

solved
 
Quote:

Originally Posted by goumba (Post 5672545)
The line length can get all screwy with escape codes, and enclosing the non printing (in this case the color) codes in the escaped brackets can help with the problem. It did so for me with my colored prompt.

goumba,
Now I see what you mean. Thanks for your patients.
This works as intended:
Code:

$ export PS1="\[\e[1;33m\][]\n$\[\e[m\] "
[]
$ echo xx
xx
[]
$

The last line is output after arrow up then arrow down. It is blank as expected.

The command prompt dissected:
Code:

export PS1="\[  \e[1;33m  \] []  \n    $  \[  \e[m      \] "
            \[ zero-length \]              \[ zero-length \]
                \e[1;33m        yellow        \e[m
                              [] newline $

Thank you all for your helps.

Habitual 02-19-2017 06:38 AM

Good job and well done.
I had to do some kungfu on my PS1 to get this a few years ago.
Code:

DOS='C:${PWD//\//\\\}>'
PS1="\[\033[00m\]\[\033[00m\]\[\033[01;39m\]$DOS\[\033[00m\]"

which I worked on from the authoritative ArchWiki article.

Have fun!

PS1 was the first thing I "programmed" in Linux.
Enjoyed it every minute.


All times are GMT -5. The time now is 08:20 PM.