ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I'm don't think it's related to your distribution as such, but it could be related to your terminal settings. Did the code I posted get rid of the [-characters, but failed to provide colour? Does your terminal support colours? What does echo $TERM produce?
This script works on my system. I'm using PuTTY to connect from a Windows system, and my $TERM variable says 'xterm'.
...and as colucix points out in post #5 and #8 below, '\033' is ASCII code 27 (escape), which is the same as \e. In other words, I originally had two escape codes in my script, which was totally unnecessary but by chance still produced the correct colours.
Last edited by Ser Olmy; 05-20-2012 at 01:44 PM.
Reason: Typo in code
Just remove the extra \[ at the beginning of each color definition. \e (escaped lowercase e) is an alias for the escape character which is octal \033. Indeed, these are called ANSI Escape Sequences. Hence the sequence:
is the same as
For a nice explanation about colors in bash, see this ArchWiki page.
Edit: according to the Advanced Bash Scripting Guide:
Use the following template for writing colored text on a colored background.
echo -e '\E[COLOR1;COLOR2mSome text goes here.'
The "\E[" begins the escape sequence. The semicolon-separated numbers "COLOR1" and "COLOR2" specify a foreground and a background color
the \E (uppercase) is allowed as well, but putting it before \033 is an unused repetition and brings to an extra control character in the output (you can easily verify using od -c).
Last edited by colucix; 05-20-2012 at 01:23 PM.
Reason: Added more information.
Also, see my revised script in post #4. I was thrown off track by the '\[' sequence, beliving it was intended to be the start of an escape sequence. Seems that it was just a superfluous character sequence, and omitting it entirely should produce the intended results.
The two escaped brackets highlighted in red are meant to begin and terminate a sequence of non-printing characters respectively. They are not really necessary and I cannot tell you in which cases they become mandatory. To be sure, try to set
PS1='\033[38;05;240m[\h@\u] \w \$ \033[00m'
and see if there is any difference in the appearance of your prompt. Said that, you can safely use the same color sequences both for your prompt and in your scripts. A complete list of allowed escape sequences (other than colors) in bash prompt, here.
The two escaped brackets highlighted in red are meant to begin and terminate a sequence of non-printing characters respectively. They are not really necessary and I cannot tell you in which cases they become mandatory.
I think I just found out: Remove the brackets, and then try to cycle through your command history with the arrow keys.
But you can still use the exact same colour sequences, just put '\[' and '\]' around them when defining PS1.