Help on controlling BASH command line

Lennart Sorensen lsorense-1wCw9BSqJbv44Nm34jS7GywD8/FfD2ys at public.gmane.org
Mon Jun 6 17:44:57 UTC 2005


On Mon, Jun 06, 2005 at 01:19:48PM -0400, John Wildberger wrote:
> On Sunday 05 June 2005 10:47 am, Lennart Sorensen wrote:
> > Bash is not aware of escape codes so it just sees a prompt consisting of
> > some 40 or 50 characters, and assumes that is how many characters wide
> > the prompt is.  So when the prompt length and the amount typed reaches
> > the width of the display, it wants to wrap and continue on another line.
> > So either you have to figure a way to tell bash the real length of the
> > prompt or use a shell that does know about escape codes.  I think zsh
> > might, but I am not sure.  I do have something similar to yours above in
> > my zsh setup and it wraps at the right location every time.  I guess
> > that means zsh does do the right thing.
> 
> Lennart,
> It appears to me that Bash is not confused by escape codes.
> Here are some sample bash lines (Mandrake 10):
> 
> 1 john [~]$ echo $PS1
> 2 \u [\w]\$
> 3 john [~]$
> 4 john [~]$ PS1="\n\e[43;1m[\h]\e[m [\u]:\e[42;1m \@ \e[m \e[44;1m [\w]  
> 5 \e[m\n\\$"
> 6
> 7 [localhost] [john]: 01:02 PM   [~]
> 8 $ here you can type a command that exceeds the display field over several 
> lines. 
> 
> line 4 changes the prompt from the original (line 3) to a deliberate complex 
> structure to give color control. The line wraps correctly over to line 5 
> without giving any problems. The four items in the new prompt are color 
> controlled. The hostname is in read, the user name is in neutral, the time is 
> in green and the current path is in blue. There is a newline at the beginning 
> and a newline at the end to permit a full line for command usage.. The \\$ at 
> the very end ensures that the $prompt changes to a # when used with 'su'.
> John

The new line is a feature of bash that it knows about, so it starts
counting fresh.  If it works without that newline in the prompt, then I
will be impressed, since I don't think bash knows about escape codes
(being escape stuff, not just bash \ codes).  Your test doesn't seem to
show anything.  Of course setting PS1= works since bash sees plain ascii
charaters being typed as a command and of course that wraps fine.  It is
when it tries to display the prompt and figure out how long it is that
things break, which your newline at the end of the prompt conviniently
avoids by starting a new prompt line with no escape codes to confuse
bash.

I personally can't imagine wanting to have a two line prompt.  Seems
inefficient.

Lennart Sorensen
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list