LinuxQuestions.org
Visit Jeremy's Blog.
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
  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
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
Old 07-19-2015, 02:38 PM   #9
ElementaryOS, DearWatson
LQ Newbie
 
Registered: Jul 2015
Posts: 2

Rep: Reputation: Disabled
Looky what I found...

Taken from an answer on the Stackexchange website titled: $PS1 on Ubuntu VM does not match actual prompt

This explains pretty much all of it, but I still don't understand the purpose of the bell character (/a). EDIT: Answered my own question. Information added. See following 2 posts for details.
The default $PS1 in Ubuntu consists of three parts:

\[\e]0;\u@\h: \w\a\]

This is an escape sequence which will set the terminal title text to $USER@$HOST: $PWD.

\[ and \] indicate the beginning and end of a sequence of non-printing characters.

\e is an ASCII escape character.

]0; is the specific escape sequence to set the terminal icon and title in xterm compatible terminals


\u expands to the username of the current user.

@ is a literal @.

\h expands to the hostname.

: is a literal colon character.

\w expands to the current working directory.

\a is an ASCII bell character. In this case, it separates the terminal title from the terminal prompt.

${debian_chroot:+($debian_chroot)}

If you're in a chroot environment, this will expand to the name of the chroot in parentheses.

${var:+OTHER} evaluates to $OTHER if var is set, otherwise as null string. $debian_chroot is a variable initalized in /etc/bash.bashrc to the contents of the file /etc/debian_chroot. Thus if your chroot environment includes this file, the prompt will include the contents of that file as an indication for which chroot the shell currently operates in.


\u@\h:\w\$

This is the actual prompt you typically see.

\u, @, \h, :, \w are as above.

\$ expands to a number sign # if the effective uid is zero (i.e. user is root), otherwise it expands to a dollar sign $.

Last edited by ElementaryOS, DearWatson; 07-19-2015 at 04:08 PM. Reason: Answered my own question, edited to append the information.
 
Old 07-19-2015, 02:58 PM   #10
Head_on_a_Stick
Senior Member
 
Registered: Dec 2014
Location: London, England
Distribution: Debian stable (and OpenBSD-current)
Posts: 1,187

Rep: Reputation: 285Reputation: 285Reputation: 285
Quote:
Originally Posted by ElementaryOS, DearWatson View Post
\a is an ASCII bell character.
This triggers the "bell" sound in the internal speaker.

You almost certainly have this disabled -- to hear it run:
Code:
modprobe pcspkr
xset b
amixer set 'Beep' 100% unmute # pure ALSA only, for a Pulseaudio systems try: amixer -D pulse set 'Beep' 100% unmute
echo -e '\a'
EDIT: Nice necrobump BTW

Last edited by Head_on_a_Stick; 07-19-2015 at 03:00 PM. Reason: typo
 
1 members found this post helpful.
Old 07-19-2015, 04:02 PM   #11
ElementaryOS, DearWatson
LQ Newbie
 
Registered: Jul 2015
Posts: 2

Rep: Reputation: Disabled
About that 'bell'...

Quote:
Originally Posted by Head_on_a_Stick View Post
This triggers the "bell" sound in the internal speaker.

You almost certainly have this disabled -- to hear it run:
Code:
modprobe pcspkr
xset b
amixer set 'Beep' 100 unmute # pure ALSA only, for a Pulseaudio systems try: amixer -D pulse set 'Beep' 100% unmute
echo -e '\a'
EDIT: Nice necrobump BTW
Well, yes and no. Turns out I answered my own question. From my research, the bell character (in this instance, /a) was ORIGINALLY used for an audible beep or a screen flash (visual bell). In modern usage it apparently is also used in other ways.

After experimenting with the PS1 variable myself, I determined that in this case the bell character (/a) separates the shell title from the shell prompt.

Believe it or not I created an account specifically to post to this thread. Being a super-noob, everything I know has been researched myself... so I figured I'd share the love 'n' learnin' . Please excuse the excessive formatting, I only mean to make it easier for those that just want the 'meat' of the information.

Last edited by ElementaryOS, DearWatson; 07-19-2015 at 08:46 PM. Reason: Incorrect information removed
 
  


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 On
HTML code is 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

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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