LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 01-26-2011, 04:14 AM   #1
armandino
Member
 
Registered: Oct 2005
Posts: 72

Rep: Reputation: 15
bash PS1 debian setting - syntax


I noticed that on my Debian system the default PS1 value for non root users is, as reported by "echo $PS1":
Code:
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$
I tried to find out some clear explanation about such a syntax (apart from the meaning of the "usual" \w,\h... codes which I already know), but I did not find anything.
More precisely, I found many documents and examples about PS1 customization, but the above syntax (particularly the ${debian_chroot:+($debian_chroot)} part) is either not used or used but not explained, as it's knowledge was taken for granted.

Can someone explain it to me or tell me where to find some clear explanation?
 
Old 01-26-2011, 04:31 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 8,994

Rep: Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347
First let's see the syntax:
Code:
${debian_chroot:+($debian_chroot)}
this is a shell parameter expansion, that can be explained as "if debian_chroot is null or unset, nothing is substituted, otherwise the expansion of $debian_chroot inside parentheses is substituted". In other words, the content of the variable debian_chroot - embedded in parentheses - is inserted into the prompt only if the variable itself is set and is not null. Otherwise a couple of empty parentheses will be displayed. You can find more details about this kind of substitution in the bash reference manual, here.

Regarding $debian_chroot it should be an alias for the chroot location, as set in /etc/debian_chroot. Not sure about this. Maybe a Debian expert might shed some light.
 
1 members found this post helpful.
Old 01-26-2011, 10:44 AM   #3
armandino
Member
 
Registered: Oct 2005
Posts: 72

Original Poster
Rep: Reputation: 15
Thanks a lot, I finally understood.
That kind of substitution is NOT explained in the bash man pages, is it?

What about the first part of the value?
Code:
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$
What is it? I saw it appears when I issue the "echo $PS1" command in a pseudo terminal (after having switched from root to user with "su user") but does not appear in ttys; why?

Finally, as it's not every day to find a... guru :], a last question: how can I, while in man pages (less), search for a word located at the beginning of a line?

Last edited by armandino; 01-26-2011 at 11:02 AM.
 
Old 01-26-2011, 11:17 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 8,994

Rep: Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347
Quote:
Originally Posted by armandino View Post
Thanks a lot, I finally understood.
That kind of substitution is NOT explained in the bash man pages, is it?
Yes, it is. You can find it in man bash, section EXPANSION, subsection Parameter Expansion. The bash man page has almost the same content of the Bash Reference Manual.

Quote:
Originally Posted by armandino View Post
What about the first part of the value?
Code:
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$
What is it? I saw it appears when I issue the "echo $PS1" command in a pseudo terminal (after having switched from root to user with "su user") but does not appear in ttys; why?
Not sure about this. Please, can you elaborate the difference between tty and pts and between user and root?

Quote:
Originally Posted by armandino View Post
Finally, as it's not every day to find a... guru :], a last question: how can I, while in man pages (less), search for a word located at the beginning of a line?
To search at the very beginning of the line, type
Code:
/^WORD_TO_SEARCH
to ignore blank spaces at the beginning:
Code:
/^ *WORD_TO_SEARCH
as for regular expressions. The only difference is that the search is case-insensitive.

Last edited by colucix; 01-26-2011 at 11:21 AM.
 
Old 01-27-2011, 12:50 PM   #5
armandino
Member
 
Registered: Oct 2005
Posts: 72

Original Poster
Rep: Reputation: 15
Thanks for your help and courtesy.
I made some more tests. The situation seems to be as follows.
If I issue the "echo $PS1" command as "armando" user in a virtual terminal (tty) I get the expected output
Code:
echo $PS1
${debian_chroot:+($debian_chroot)}\u@\h:\w\$
If I issue the same command in a pseudo terminal in Gnome (pts/...) I get the "strange" one:
Code:
echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$
I tried both logging in to Gnome as "armando" and also logging in as root, opening a pts terminal and then "becoming" armando by means of "su armando". No difference.
 
Old 02-04-2011, 06:48 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 8,994

Rep: Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347
Sorry for late... I missed your last post!

Indeed it looks weird. The first part does not make sense to me:
Code:
\[\e]0;
since it's not the usual color code. Anyway it looks like the PS1 variable is somehow modified by retaining the old value. Take a look at /etc/profile, /etc/bash.bashrc and eventually to your custom $HOME/.bashrc.

/etc/profile is the first configuration file sourced at login, where PS1 is set for interactive shells as
Code:
PS1='\u@\h:\w\$ '
then in /etc/bash.bashrc the debian_chroot part is appended at the beginning:
Code:
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
After that something checks if you're running in a pseudo-terminal and does a mistake, like:
Code:
PS1='\[\e]0;\u@\h: \w\a\]'$PS1
Please, see if you can find something similar somewhere, then we can try to investigate the reasons and the exact meaning. Cheers!
 
Old 02-04-2011, 07:31 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 8,994

Rep: Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347
An update: looking at the Bash Prompt HOWTO, here, it looks like the escape sequence at the beginning is meant to set the terminal title. This is the reason why only pseudo-terminal allocations bring to that apparently weird prompt (actually it was my ignorance).

In the ANSI escape sequences the symbol \[ begins a sequence of non-printing characters. The symbol \] terminates it. So the part in grey:
Code:
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$
is a non-printing sequence prepended to the actual prompt specification to set the window title. You can demonstrate it if you change one of the items, for example:
Code:
\[\e]0;\w@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$
so that you will see the current working directory in place of the username. The only part I cannot explain in details is
Code:
\e]0;
the closing square bracket puzzles me. Maybe someone else can shed some light.
 
Old 04-07-2011, 02:04 AM   #8
tankdthedruid
LQ Newbie
 
Registered: Apr 2011
Posts: 1

Rep: Reputation: 1
Sorry for digging up an older thread but, I believe that this portion of the PS1 value:
Code:
\[\e]0;
is a reset sequence for foreground, background, and boldness color to their default values for the "Window Title" of the pseudo-term. (Most likely specified in /etc/bash.bashrc). I believe it can be read as:

\[
{begin non-print chars}

\e]0;
{"Window Title" color and boldness default}

\u@\h: \w\a\]
{Set term title as - $USER@$HOSTNAME: $PWD#}

This is all kind of hazed speculation seeing as my days 'beautifying' my Xterm are long gone.

Goodluck.
 
1 members found this post helpful.
  


Reply

Tags
bash, command prompt, customize


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting a Custom PS1 For Users? carlosinfl Red Hat 4 09-11-2010 10:11 AM
PS1 = * in bash, where '*' = parameters BlueSpirit Slackware 5 11-05-2006 08:21 AM
bash PS1 problems Krelian Linux - General 2 10-07-2006 09:16 PM
Problem setting the PS1 enviroment gavita Red Hat 1 12-07-2005 04:59 PM
help i can't save my PS1 setting Paxmaster Linux - Software 3 07-17-2004 10:39 PM


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