Decryption of PS1 statement

Questions about other topics - please check if your question fits better in another category before posting here
Forum rules
Before you post please read how to get help
Post Reply
Lady3mlnm
Level 1
Level 1
Posts: 2
Joined: Sun Nov 04, 2018 4:25 am
Location: Moscow, Russia
Contact:

Decryption of PS1 statement

Post by Lady3mlnm » Sun Nov 04, 2018 5:40 am

Hi! I'm only several months on Linux, but I don't think that my question is "newbie".

I tried to completely understand what symbols in PS1 statement of the terminal mean. I use Linux Mint 19.0 Cinnamon with default terminal "Gnome-terminal" and default theme. Command echo $PS1 displays the next contents:

Code: Select all

\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$
I tried to search but found only simple superficial answers that don't give full picture. Can anyone spend his/her time to fully explain the work of PS1-statement in detail?

The issue that I already understand:
\u - user name
\h - host name
\w - current working directory
\$ - if the effective UID ("user identifier") is 0 (i.e. root user), then pring #, otherwise print $

\e - an ASCII escape character (033)
\a - an ASCII bell character (07) (Bell character is the control code used to sound an audible bell or tone in order to alert the user. When it is sent to a printer or a terminal, nothing is printed, but an audible signal is emitted instead.)
\nnn - the character corresponding to the octal number nnn
(=> \033 corresponds to /e - an ASCII escape character)

\[ - begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
\] - end a sequence of non-printing characters

${ - start of the environment variable substition
} - end of the environment substition
Code ${debian_chroot:+($debian_chroot)} means
"if $debian_chroot is defined then use $debian_chroot else do nothing"
(usually $debian_chroot on user computer is not defined)

If I understand correctly,
\[\033[ means start color scheme
m\] means stop color scheme
0;32 - green color
01;32 - increase font weight and change color shade
0;34 - blue color
01;34 - increase font weight and change color shade
00 - white color

If it's all right then I've got the part of PS1-statement after $.
But what means symbols before it?
More specifically, what means "\[\e]0;"?
Why in this part we have duplicated "\u@\h: \w" and sound notification that is usually never heard?

I will be glad to any additional explanations of PS1-statement.

User avatar
catweazel
Level 17
Level 17
Posts: 7774
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: Decryption of PS1 statement

Post by catweazel » Mon Nov 05, 2018 1:10 am

Lady3mlnm wrote:
Sun Nov 04, 2018 5:40 am
But what means symbols before it?
More specifically, what means "\[\e]0;"?
Why in this part we have duplicated "\u@\h: \w" and sound notification that is usually never heard?
http://tldp.org/HOWTO/Bash-Prompt-HOWTO/index.html
¡uʍop ǝpısdn sı buıɥʇʎɹǝʌǝ os ɐıןɐɹʇsnɐ ɯoɹɟ ɯ,ı

Lady3mlnm
Level 1
Level 1
Posts: 2
Joined: Sun Nov 04, 2018 4:25 am
Location: Moscow, Russia
Contact:

Re: Decryption of PS1 statement

Post by Lady3mlnm » Mon Nov 05, 2018 9:42 am

Oh, thank you, thank you, thank you!

I got, that the part \[\e]0;\u@\h: \w\a\] determines the appearance of the terminal title bar.
Here \a is not a sound notification but a conditional character of the code block end.
So everything that is written between \[\e]0; and \a\] appears in the terminal title bar.
I'm still not sure if 0; has some separate meaning or it is a part of conditional character sequence, but I don't consider this question very important to me.

Couple useful links for PS1 modifications.
Bash prompt escape sequences:
http://tldp.org/HOWTO/Bash-Prompt-HOWTO ... ences.html
http://man7.org/linux/man-pages/man1/ba ... #PROMPTING

List of color sequences:
http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
http://man7.org/linux/man-pages/man5/te ... LES_FORMAT
(copy quote: "...to be strictly accurate, we must mention that the list above is for colours at the console. In an xterm, the code 1;31 isn't "Light Red," but "Bold Red." This is true of all the colours.")


---
Now my terminal is more neat, convenient and pet. :)

Post Reply

Return to “Other topics”