0

I have a bash command prompt like this:

someone@some-host:~$

I checked the PS1:

echo $PS1

It shows this:

\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$

How to interpret it? Why is it so complex?

I thought "\u@\h:\w$" should be enough. And I tried to set PS1 to it. It works the same or at least looks so.

smwikipedia
  • 61,609
  • 92
  • 309
  • 482
  • Consider [unix.se] for questions more about interactive use and configuration than software development. – Charles Duffy Nov 10 '21 at 00:11
  • 1
    Please post the output of `printf "%q\n" "$PS1"` – KamilCuk Nov 10 '21 at 00:12
  • (that said, some of this is obvious -- f/e, if you're in a chroot, this puts it in the prompt; and the sequence at the very beginning, I'd guess, resets any active color codes left over from prior commands -- though one would need to go look up an ANSI terminal sequence reference to be sure) – Charles Duffy Nov 10 '21 at 00:12
  • (and yes, as KamilCuk says, `echo $PS1` is pretty wildly unreliable -- see [I just assigned a variable, but `echo $variable` prints something else](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else), and [Why is printf better than echo?](https://unix.stackexchange.com/a/65819/3113) over at [unix.se]) – Charles Duffy Nov 10 '21 at 00:13
  • 1
    To learn about the syntax used by `PS1` See: [`man bash` section `PROMPTING`](https://man7.org/linux/man-pages/man1/bash.1.html#PROMPTING) – Léa Gris Nov 10 '21 at 00:13
  • 2
    And `\[...]` is used around escape sequences to prevent them from counting against the column count. – Barmar Nov 10 '21 at 00:13
  • ...in large part, btw, this reads as a "please explain this code" question, and those are pretty categorically closed as "too broad". – Charles Duffy Nov 10 '21 at 00:14
  • 1
    [Prompt Magic](https://www.funtoo.org/Prompt_Magic) is a good (non-official) prompt reference. See "PROMPTING" in [man 1 bash](https://man7.org/linux/man-pages/man1/bash.1.html) for an official reference. – David C. Rankin Nov 10 '21 at 00:14
  • _I thought "\u@\h:\w$" should be enough._ : If it is good enough for you, why don't you set PS1 in that way? You are in the driver's seat, and you decide how your prompt should look like. – user1934428 Nov 10 '21 at 07:30
  • @user1934428 I am not a bash expert. I have tried that though a bit worried about the side effects. – smwikipedia Nov 11 '21 at 02:47

1 Answers1

2

How to interpret it?

\[ ... \] is interpreted by Bash as not visible. It matters if you want for example Home key to jump to the beginning of the line but right after prompt. Bash has to know, how many characters to jump to place the cursor right after the prompt, so it has to know how many characters were printed when printing the prompt. https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Controlling-the-Prompt

\e]0;\u@\h: \w\a sets the title of your terminal. https://en.wikipedia.org/wiki/ANSI_escape_code#OSC_(Operating_System_Command)_sequences

${debian_chroot:+($debian_chroot)} Expands to the string (<value of debian_chroot>) when variable debian_chroot is set, otherwise expands to nothing. Basically it adds ( ) to the value of debian_chroot, when it is set to display it nicely. https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameter-Expansion

KamilCuk
  • 120,984
  • 8
  • 59
  • 111