LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   bash PS1 debian setting - syntax (https://www.linuxquestions.org/questions/linux-general-1/bash-ps1-debian-setting-syntax-858718/)

armandino 01-26-2011 04:14 AM

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?

colucix 01-26-2011 04:31 AM

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.

armandino 01-26-2011 10:44 AM

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?

colucix 01-26-2011 11:17 AM

Quote:

Originally Posted by armandino (Post 4238368)
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 (Post 4238368)
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 (Post 4238368)
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.

armandino 01-27-2011 12:50 PM

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.

colucix 02-04-2011 06:48 AM

Sorry for late... I missed your last post! :redface:

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!

colucix 02-04-2011 07:31 AM

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.

tankdthedruid 04-07-2011 02:04 AM

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.

ElementaryOS, DearWatson 07-19-2015 02:38 PM

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 $.

Head_on_a_Stick 07-19-2015 02:58 PM

Quote:

Originally Posted by ElementaryOS, DearWatson (Post 5393544)
\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

ElementaryOS, DearWatson 07-19-2015 04:02 PM

About that 'bell'...
 
Quote:

Originally Posted by Head_on_a_Stick (Post 5393549)
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.


All times are GMT -5. The time now is 03:22 AM.