LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-28-2009, 08:01 PM   #1
hungrigerhaifisch
Member
 
Registered: Aug 2005
Distribution: Arch Linux
Posts: 91

Rep: Reputation: 15
Lightbulb [solved]bash-escape-problem


Hello.
I have the following line in my ~/.bashrc

Code:
PS1='\[\e[0;32m\]\u\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \[\e[m\] \[\e[1;32m\]\$ \[\e[m\]\[\e[1;37m\] '
This is causing commands that 'overflow' a single line to get displayed incorrectly, and it also effects, my bash_history.
I'm guessing that there's a typo in there somewhere, anyone spot it??

Also, I'd like to append the current time (like [02:00]) to the far right of any command I input.

Any quick fixes around?

Thanks

Last edited by hungrigerhaifisch; 10-31-2009 at 08:11 PM.
 
Old 10-29-2009, 04:49 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by hungrigerhaifisch View Post
Hello.
I have the following line in my ~/.bashrc

Code:
PS1='\[\e[0;32m\]\u\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \[\e[m\] \[\e[1;32m\]\$ \[\e[m\]\[\e[1;37m\] '
This is causing commands that 'overflow' a single line to get displayed incorrectly, and it also effects, my bash_history.
I'm guessing that there's a typo in there somewhere, anyone spot it??

Also, I'd like to append the current time (like [02:00]) to the far right of any command I input.

Any quick fixes around?

Thanks
I don't know about the time, but you'll get problems with the prompt if you don't leave "it" with the default colour - i.e. end the prompt with "\[\e[m\]" instead of "\[\e[1;37m\]". If you want the text to be a different colour, there should be a property in your terminal emulator's settings to do that.

John G
 
Old 10-29-2009, 07:06 AM   #3
hungrigerhaifisch
Member
 
Registered: Aug 2005
Distribution: Arch Linux
Posts: 91

Original Poster
Rep: Reputation: 15
Quote:
"\[\e[m\]" instead of "\[\e[1;37m\]"
So simple, thanks, that fixed it!

Anyone know about the time thing? I saw this on some screenshot somewhere, and I'd really like to try it, so if anyone has an idea, I'd be a happy puppy
 
Old 10-29-2009, 07:31 AM   #4
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
You got me interested now - http://tldp.org/HOWTO/Bash-Prompt-HO...sequences.html
 
Old 10-29-2009, 10:21 AM   #5
hungrigerhaifisch
Member
 
Registered: Aug 2005
Distribution: Arch Linux
Posts: 91

Original Poster
Rep: Reputation: 15
Thanks for the tip.
But I still haven't found out how I can do what I want...

Basically I think I saw something like this:

Code:
user wd $ somecommand                    [current time]
I guess that this would involve somekind of clever scripting, but I'm at a total loss in that area...

EDIT:

I've sadly discovered, that my problem (lines overflowing, this means, when a given line gets to long, it should just wrap around onto the next line, instead it just overwrites my bashpromt) is still there.

Here my line:
Code:
PS1='\[\e[0;32m\]\u\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \[\e[m\] \[\e[1;32m\]\$ \[\e[m\] '

Last edited by hungrigerhaifisch; 10-29-2009 at 01:09 PM. Reason: problem not solved
 
Old 10-29-2009, 05:51 PM   #6
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 48
I believe that \t will display time.

Here's my prompt:

Code:
 Thu Oct 29 17:12:15 bchittenden@cribbage:~/bin
>
$PS1 is set to

Code:
\e]0;\h \w\007 \d \t \u@\[\e[1;33m\]\h\[\e[0m\]:\w\n>
I set the color of the hostname according to which drives are mounted: green for pre-production only, yellow for prod and prep, red for production.

"\n> " at the end of the prompt means that I always have an entire line of text to write my commands on... vertical screen real estate isn't as important as getting more of the command on one line.

Oh, by the way, '\e]0;\h \w\007' sets the window title to be my current directory.

Last edited by bartonski; 10-29-2009 at 06:05 PM.
 
Old 10-29-2009, 06:02 PM   #7
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 48
Just to make things a little cleaner, I would be inclined to do the following:

Code:
username='\e[0;32m\]\u\[\e[m\]'
wd='\[\e[1;34m\]\w\[\e[m\]'    #wd: working directory
PS1="$username $wd \$ "
Ok, the dollar sign in the prompt doesn't end up green, but otherwise, it's identical, and a lot easier to read in the .bashrc
 
Old 10-29-2009, 07:22 PM   #8
hungrigerhaifisch
Member
 
Registered: Aug 2005
Distribution: Arch Linux
Posts: 91

Original Poster
Rep: Reputation: 15
Nice idea with the variables, makes things a little simpler

BUT: I'm still having this problem with the 'current-line' getting overwritten should the command become too long. This is highly annoying.
Removing any customisation I've done, results in correct behaviour, so there must me something wrong with those lines...
 
Old 10-29-2009, 11:27 PM   #9
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 48
Quote:
Originally Posted by hungrigerhaifisch View Post
Nice idea with the variables, makes things a little simpler

BUT: I'm still having this problem with the 'current-line' getting overwritten should the command become too long. This is highly annoying.
Removing any customisation I've done, results in correct behaviour, so there must me something wrong with those lines...
I set the prompt first to "PS1="$username\$ ", which displayed the problem, then to "$wd \$", which did not.

I went on to split apart '\e[0;32m\]\u\[\e[m\]', and then realized that the reason that I saw the problem is not exactly why you saw the problem:

in

Code:
username='\e[0;32m\]\u\[\e[m\]'
, I left off the initial '\['. The pair of '\[' and \]', as I'm sure that you are aware, causes the contents to be counted as non-printable, i.e. 0 length by bash. If you have these unbalanced, Bash apparently doesn't know what's printable and what's not, causes the length of the string to be mis-calculated. Because it doesn't know how long the string is, it doesn't know where to wrap at the end of the line.

I suspect that you have a much more subtle version of the same error lurking in your prompt somewhere, perhaps a '\[' or '\]' that is being masked by an extra escape character or slash, causing an imbalance which throws off the calculated length of the string.

Check here for more details on non-printable characters in prompts: http://tldp.org/HOWTO/Bash-Prompt-HO...tingchars.html

Cheers, old chum!

http://downhilladventures.com/images...rk%20photo.jpg

Last edited by bartonski; 10-29-2009 at 11:28 PM.
 
Old 10-31-2009, 09:06 AM   #10
hungrigerhaifisch
Member
 
Registered: Aug 2005
Distribution: Arch Linux
Posts: 91

Original Poster
Rep: Reputation: 15
Lightbulb

Hey, thanks for that!

I think its working now.

It was just missing the '\[' infront of the first '\e' in username

And, I like the picture
 
  


Reply



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
escape string in bash script so it can be used in command line BuckRogers01 Linux - Software 15 08-12-2010 09:38 AM
Problem in getting telnet client prompt by pressing Escape character( ctrl -] ) Prassanta SUSE / openSUSE 0 01-23-2007 12:33 AM
bash - escape char problem ddaas Programming 9 11-11-2005 06:08 AM
how to escape variable in bash??? rabbate Linux - General 4 03-17-2004 08:04 PM
smiley face escape sequence problem mandrakeroot Programming 12 09-06-2003 06:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:02 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