LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 11-01-2014, 03:51 PM   #1
MisterSnrub
LQ Newbie
 
Registered: Mar 2013
Posts: 6

Rep: Reputation: Disabled
How to add color to your bash prompt without screwing up long commands?


Normally my PS1 in bash is set as the following:

Code:
[mrsnrub@my-test-site ~]$ echo $PS1
[\u@\h \W]\$
With this normal setting I can run very long commands, like the following:

Code:
[mrsnrub@my-test-site ~]$ echo "We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility,"
We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility,
However, if I change PS1 to yellow text, like so:

Code:
[mrsnrub@my-test-site ~]$ export PS1="\e[1,33m[\u@\h: \W]\$ \e[m"
...I can no longer run very long commands:

As soon as the screen reaches what should be a line break, the text doesn't break, and the cursor overwrites the beginning of the same line:

Code:
 Tranquilmy-test-site ~]$ echo "We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic
Why does this happen when all I'm doing is changing the color of the prompt?
 
Old 11-01-2014, 07:02 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
When your prompt string contains what would normally be printing characters that are actually part of an escape sequence, you need to bracket that portion of the string with the 2-character sequences \[ and \] to tell the shell that those characters do not advance the cursor position.
Code:
export PS1="\[\e[1;33m\][\u@\h: \W]\$ \[\e[m\]"

Last edited by rknichols; 11-01-2014 at 07:15 PM. Reason: Highlight changes
 
1 members found this post helpful.
Old 11-02-2014, 09:36 AM   #3
MisterSnrub
LQ Newbie
 
Registered: Mar 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Perfect!! Thank you so much!!
 
Old 11-03-2014, 10:22 AM   #4
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
If you regularly resize your terminal windows, you might want to also add: shopt -s checkwinsize to your .bashrc. as that can also help bash to keep track of how long the terminal line is.
 
1 members found this post helpful.
  


Reply

Tags
bash, ps1



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
Changing Bash Color Prompt carlosinfl Red Hat 2 03-25-2010 01:16 AM
[SOLVED] Color bash prompt MTK358 Linux - Software 8 12-06-2009 07:26 AM
please help make my bash prompt color extendedping Linux - Software 4 04-02-2009 09:47 PM
Using BASH redirects and "prompt" files to interact with commands/scripts ?? swedish_lunacy Linux - Newbie 22 07-27-2006 08:33 AM
help! bash command prompt font/color change richiangpan9 Linux - Newbie 3 04-24-2005 11:34 AM

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

All times are GMT -5. The time now is 10:44 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
Open Source Consulting | Domain Registration