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 read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
Lady3mlnm
Level 1
Level 1
Posts: 7
Joined: Sun Nov 04, 2018 4:25 am
Location: Moscow, Russia
Contact:

Decryption of PS1 statement

Post by Lady3mlnm »

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.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
catweazel
Level 19
Level 19
Posts: 9763
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: Decryption of PS1 statement

Post by catweazel »

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
"There is, ultimately, only one truth -- cogito, ergo sum -- everything else is an assumption." - Me, my swansong.
Lady3mlnm
Level 1
Level 1
Posts: 7
Joined: Sun Nov 04, 2018 4:25 am
Location: Moscow, Russia
Contact:

Re: Decryption of PS1 statement

Post by Lady3mlnm »

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. :)
User avatar
BG405
Level 9
Level 9
Posts: 2508
Joined: Fri Mar 11, 2016 3:09 pm
Location: England

Re: Decryption of PS1 statement

Post by BG405 »

A bit of a late reply; was going to start a thread on this. Thanks for the info! Surprised I missed this thread before.

Here's one of my custom PS1 settings:

Code: Select all

        PS1='\[\033[01;31m\][\h\[\033[01;36m\] \W\[\033[01;31m\]]\$\[\033[00m\] '
        PS1='\[\033[01;32m\][\u@\[\033[01;31m\]\h\[\033[01;37m\] \W\[\033[01;32m\]$

I use a different colour scheme for each machine & account. This makes it so much easier to see at a glance which machine one is logged in to, in any given Terminal window, thereby (hopefully) avoiding erroneously performing commands on the wrong machine .. :oops: :mrgreen:
Dell Inspiron 1525 - LM17.3 CE 64-------------------Lenovo T440 - Manjaro KDE with Mint VMs
Toshiba NB250 - Manjaro KDE------------------------Acer Aspire One D255E - LM21.3 Xfce
Acer Aspire E11 ES1-111M - LM18.2 KDE 64 ----Two ROMS don't make a WRITE
Locked

Return to “Other topics”