Help reading this PS1 prompt setting

Forum rules
Before you post please read how to get help
Post Reply
endusermint
Level 1
Level 1
Posts: 33
Joined: Thu Mar 05, 2015 6:24 pm

Help reading this PS1 prompt setting

Post by endusermint » Mon May 14, 2018 6:48 pm

I am having trouble reading this ps1 setting. when I echo $PS1 is see:

\[\e]0;\u@\h \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\] \[\033[01;34m\]\w \$\[\033[00m\]

I know the e indicates a color setting for user at home, but i'm not sure what that debian stuff is about ( maybe referencing debian default settings?) I know the m is supposed to indicate the end of a color setting. but I have a difficult time reading this.

Could someone break this up for me, so each different item being set is on a separate line? Is it just each thing between a [xxx] counts as one item? I don't understand how come there isn't an equal number of opposite brackets. shouldn't there be something like a [[ or ]] for some of those?

I probably need to comment the hell out of the file until I can read the command line better

I'm hoping to try rearranging things like the date and its order, the current time, and things like the current working directory until i find a nice fit. Im hoping that by breaking them up line by line I can simply remove elements and add them without breaking something else. so it reads something akin to:

#Color of promt
xxxxx

#current working directory
xxxxx

#next thing I want to try out
xxxxx

any help would be greatly appreciated.

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

Re: Help reading this PS1 prompt setting

Post by catweazel » Tue May 15, 2018 1:27 am

endusermint wrote:
Mon May 14, 2018 6:48 pm
any help would be greatly appreciated.
https://www.howtogeek.com/307701/how-to ... sh-prompt/
Caution: Dancing Wu Li Master and Official curmudgeon-in-chief

rene
Level 8
Level 8
Posts: 2085
Joined: Sun Mar 27, 2016 6:58 pm

Re: Help reading this PS1 prompt setting

Post by rene » Tue May 15, 2018 8:11 am

The main documentation regarding PS1 is in the bash manpage. section PROMPTING: man bash, 1732g. The ${debian_chroot:+($debian_chroot)} is a bash parameter expansion: man bash, 918g, and means "if the environment variable $debian_chroot is not null or unset, run it as a command and insert its output here": also see "command substitution" just below the parameter expansion bit.

That is, the bash manpage should be your first stop for stuff like this. It's long and unwieldy, hence the (Mint 18-specific) line numbers above, but a good source of information. Also note by the way that changing PS1 you'd do in ~/.bashrc.

[EDIT] Didn't earlier follow catweazel's link but that's actually surprisingly complete. "Bash manpage is the main source" is still useful I guess but otherwise the link seems nice enough.

User avatar
Termy
Level 5
Level 5
Posts: 762
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Help reading this PS1 prompt setting

Post by Termy » Wed May 23, 2018 6:47 pm

Sure.

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

This just sets the terminal window title text.

Because of the tricky nature of the PS1 prompt, escape sequences need to be protected by escaped brackets, \[\]; this is just for the PS1 prompt; possibly other PS* prompts too.

\e]0;TITLE\a

This part sets the title bar text of the terminal window, where 'TITLE' would be whatever you want. The prompt has opted for the usual 'USER@HOST CWD', where CWD is the current working directory. The \e]; sets it up to change the title to whatever you want, and \a simply closes it, saying you've finished setting the title text.

Since this is being set with each PS1 instance, the title bar will always be updated. It's just a nice touch in a traditional setup. Sadly lost on me, as I don't use title bars, but it's still cool to keep in mind.

${debian_chroot:+($debian_chroot)}

Not sure, as I never really saw it in action. I can't stand the default prompt, so I always change it. As a guess, I'd say it tells you if you're chrooted in (don't worry if that makes no sense) or not. What I can tell you, is that it's bash parameter expansion, setting (but not actually assigning) a default value, if it's not already set or empty.

\[\033[01;32m\]

Sets the foreground color to green. Unusual that it uses \033 (sequence for the escape key) yet uses \e (same thing) for the title change. Seems like an oversight which has stuck around for a long time. There's also no need for the leading 0 in the 01 part. Oh well.

\u@\h

The \u is for the current user ($USER), @ is just to say where the user is, and \h is for the current host ($HOSTNAME).

\[\033[00m\]

Sets the foreground and background color to the default, which is typically white on black. This is utterly pointless, as far as I can see, since it changes the color again anyway. lol Who wrote this?! :roll:

\[\033[01;34m\]\w \$\[\033[00m\]

Changes the color to grey, then \w is for the current directory. The \$ is just for displaying the traditional prompt leader, like the >>> in an interactive Python shell, if you're familiar with that, or the : in less or vim's command mode. If you're running as a non-root user, it'll show as $, otherwise it'll show as #. The last part just sets the color back to normal again.
Here to help.

I'm LearnLinux (LL) on YouTube: https://www.youtube.com/channel/UCfp-lN ... naEE6NtDSg
I'm also terminalforlife (TFL) on GitHub: https://github.com/terminalforlife

endusermint
Level 1
Level 1
Posts: 33
Joined: Thu Mar 05, 2015 6:24 pm

Re: Help reading this PS1 prompt setting

Post by endusermint » Fri Jun 01, 2018 11:44 pm

thank you so much. very helpful replies.

Post Reply

Return to “Scripts & Bash”