LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-17-2017, 11:26 PM   #1
wolfv
LQ Newbie
 
Registered: Feb 2010
Posts: 26

Rep: Reputation: 1
Question 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.
 
Old 02-17-2017, 11:29 PM   #2
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Debian, Arch
Posts: 1,164
Blog Entries: 7

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Bash can be funny with escape codes.

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

Last edited by goumba; 02-17-2017 at 11:33 PM.
 
Old 02-18-2017, 12:28 AM   #3
wolfv
LQ Newbie
 
Registered: Feb 2010
Posts: 26

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by goumba View Post
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 \].
 
Old 02-18-2017, 02:26 AM   #4
wolfv
LQ Newbie
 
Registered: Feb 2010
Posts: 26

Original Poster
Rep: Reputation: 1
This works:
Code:
export PS1="\e[1;33m[]\e[m\n\$ "
The '$' is not colored, but that's OK.
 
Old 02-18-2017, 03:49 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,627

Rep: Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943
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.
 
Old 02-18-2017, 07:12 AM   #6
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Yawnstown, Ohio
Distribution: Mojave
Posts: 9,342
Blog Entries: 36

Rep: Reputation: Disabled
http://bashrcgenerator.com/
 
2 members found this post helpful.
Old 02-18-2017, 07:44 AM   #7
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Debian, Arch
Posts: 1,164
Blog Entries: 7

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Quote:
Originally Posted by wolfv View Post
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.
 
Old 02-18-2017, 07:51 AM   #8
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 3,612
Blog Entries: 1

Rep: Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782
Try \033 instead of \e. What I had to do for a bash script that does color. It's the octal value for e.
 
Old 02-18-2017, 11:18 AM   #9
wolfv
LQ Newbie
 
Registered: Feb 2010
Posts: 26

Original Poster
Rep: Reputation: 1
Thumbs up solved

Quote:
Originally Posted by goumba View Post
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.

Last edited by wolfv; 02-18-2017 at 11:35 AM.
 
Old 02-19-2017, 07:38 AM   #10
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Yawnstown, Ohio
Distribution: Mojave
Posts: 9,342
Blog Entries: 36

Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
910608 - command line prompt in color hamidi2 Linux - Newbie 11 09-01-2012 11:35 PM
bash script ? Change PS1 color on per user basis tommyttt Linux - Software 5 03-01-2010 01:12 AM
[SOLVED] Color bash prompt MTK358 Linux - Software 8 12-06-2009 08:26 AM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 12:33 PM
Post your BASH prompt [PS1]? introuble General 11 12-27-2006 04:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration