LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 06-14-2009, 07:30 AM   #1
JimHughen
Member
 
Registered: Jun 2009
Location: Austin, Texas
Distribution: Ubuntu
Posts: 44

Rep: Reputation: 16
Question PS1 bracket expressions?


After a cygwin install, the PS1 variable looks like:
\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$

Within this syntax looks like 2 bracket expressions:
\[\e]0;\w\a\] \[\e[32m\]\u ---> [e]0;wa] [e32m]
^......^ ^....^

These expressions appear to expand to user_name@host_name.
I am having difficulty finding where
these bracket expressions are defined/documented.

Any ideas?

thanks ...Jim Hughen
 
Old 06-14-2009, 08:22 AM   #2
JimHughen
Member
 
Registered: Jun 2009
Location: Austin, Texas
Distribution: Ubuntu
Posts: 44

Original Poster
Rep: Reputation: 16
Lightbulb

Ok, it looks like the "\e" is an ASCII ESC character.

So the following characters must be part of an ESC sequence.

Where are they defined?

...Jim Hughen
 
Old 06-14-2009, 08:48 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
These are ascii/ansi escape code control sequences. They are used for coloring and formatting text in a terminal environment. All escape sequences start with a '\033' escape code, of which '\e' is a shortcut in bash. After that comes a code for color or other text manipulation. In this example, '\e[32m' is the code for green text, and \e[0m' is the code for clearing the formatting at the end of the string.

Bash furthermore uses '\[' and '\]' for enclosing these escape sequences in the PS1 prompt. Without them the terminal can do strange things, especially when it comes to line-wrapping.

Finally, I believe the '\e]0;' sequence is for setting the xterm title formatting, but I'm not 100% sure about that. Every example I've seen follows the xterm string with a \007

Edit: Aah, '\a' is the bash equivilent to the '\007' bell code. So yes, the first part of the sequence is the xterm title setting.

Edit2: Look at 'man bash' for a detailed list of PS1 sequences. And this page gives you a multitude of examples on how to use them in PS1.

Last edited by David the H.; 06-14-2009 at 09:02 AM.
 
Old 06-14-2009, 11:42 AM   #4
JimHughen
Member
 
Registered: Jun 2009
Location: Austin, Texas
Distribution: Ubuntu
Posts: 44

Original Poster
Rep: Reputation: 16
Smile

Thanks a bunch David for this help.

I am familiar with ANSI character sequences used in other applications.

I followed the link you supplied to
http://en.wikipedia.org/wiki/ANSI_escape_code
and
http://invisible-island.net/xterm/ctlseqs/ctlseqs.html

This seems like a good reference, especially the CSI Control Sequence Introducer including the open bracket.

Colors and cursor movement on the ANSI terminal seem the main focus.

ESC[0;wa]
ESC[32m]
ESC[33m]

These (previous) must all be related to the color at and text style at that point.

I finally see:
\u - PS1 command for user name
\h - PS1 command for computer/terminal name(?)

The docs for all of this is spread out some. Because of the work I am planning to do, I need a pretty clear understanding of the ESC codes.

thanks again, ...Jim Hughen
 
Old 06-14-2009, 02:43 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Well, to tell the truth, I've only recently discovered them myself, and only just learned how to use them in PS1 a few days ago. It seems that the exact phrasing of the sequences depends on the environment you use, but it's not so hard once you know them.

Let's break this example up to make it clearer what each part is doing.

Code:
\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$

# The first part sets the xterm title (usually shows in the titlebar of the window).

\[        -Starts a sequence of escapes.
\e]0;     -Starts the xterm title prompt expression.
\w        -Display the current working directory.
\a        -Equal to \007 (system bell).  In this case used to end the xterm title prompt.
\]        -End escape sequence.

# The second part sets the actual PS1 prompt

\n        -Start with a newline.
\[        -Start another sequence of escapes.
\e[32m    -Sets the color to green.
\]        -End escape sequence.

\u@\h     -User at host name.  This and the \w below are the visible parts of the prompt. 

\[        -Begin another escape sequence.  
\e[33m    -Set color to red.
\]        -End escape sequence.

\w        -Display working directory in prompt.

\[        -Begin another sequence.
\e[0m     -Reset escape formatting to default.
\]        -End sequence.

\n        -Another newline.
\$        -The final command line prompt character.
I hope this makes it all clearer to you.

Last edited by David the H.; 06-14-2009 at 02:50 PM. Reason: Made some minor edits
 
Old 06-16-2009, 03:33 PM   #6
JimHughen
Member
 
Registered: Jun 2009
Location: Austin, Texas
Distribution: Ubuntu
Posts: 44

Original Poster
Rep: Reputation: 16
Arrow Bash ESC sequences - case sensitivety

Thanks David the H. for your response.

I notice the \a that is closing the "\[\e]0;\w\a\]\n" part of PS1 (which is an \007 and a bell) is necessary for this command to write into the window header.

\[ -Starts a sequence of escapes.
\e]0; -Starts the xterm title prompt expression.
\w -Display the current working directory.
\a -Equal to \007 (system bell).
In this case used to end the xterm title prompt.
\] -End escape sequence.

I cannot find a doc reference that discusses this use of the bell character. I have looked at:
--> http://en.tldp.org/HOWTO/Bash-Prompt...sequences.html
--> http://wiki.archlinux.org/index.php/Color_Bash_Prompt

(maybe I am just missing it)

=====================
Noticed something interesting:
Generally, it seems Linux is case sensitive for path/filenames.
However, a "cd /Bin" does work on my cygwin; and does navigate to the correct /bin folder. But the prompt now shows that directory as though it were "/Bin", but is not...???

Is this a bug or a feature?

...Jim Hughen
 
Old 06-16-2009, 09:37 PM   #7
BeacoN
Member
 
Registered: Nov 2008
Distribution: Linux Mint
Posts: 56

Rep: Reputation: 17
I found this site about a year ago that introduced me to this....

http://www.ibm.com/developerworks/li.../l-tip-prompt/

my prompts have been looking pretty freakin sweet. It even tells how to change the title of the windows and other things.
 
Old 06-17-2009, 01:45 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Quote:
Originally Posted by JimHughen View Post
I cannot find a doc reference that discusses this use of the bell character.
Truthfully, I've never seen one either. All I know is that every example I've ever seen ends the sequence with it, so I must assume it's mandatory. I'd love to see some clear confirmation of it though.

And thanks for finding the tldp page. That's a great reference.

Quote:
However, a "cd /Bin" does work on my cygwin; and does navigate to the correct /bin folder. But the prompt now shows that directory as though it were "/Bin", but is not...???

Is this a bug or a feature?
I don't know anything about cygwin, but considering that it runs on the case-insensitive OS, I can only imagine that it's been deliberately set up that way. It could be that they've simply defined Bin as a symlink to bin. If I create a symlink to a directory on my system and cd into it, my prompt also displays the symlink name instead of the real name.

Last edited by David the H.; 06-17-2009 at 01:46 PM.
 
  


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
URLs ending in a bracket ) aren't parsed properly pwc101 LQ Suggestions & Feedback 2 06-12-2009 09:50 AM
Tournament Bracket anamericanjoe General 1 09-19-2006 04:50 PM
The "angle-bracket" key not working? riwaJR Linux - General 1 04-02-2006 05:42 PM
Tcl/Tk Bracket Problems in Command Kenji Miyamoto Programming 0 06-26-2005 11:29 PM
Non-functioning bracket-key dekket Linux - General 1 04-21-2004 06:46 PM


All times are GMT -5. The time now is 01:14 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration