A
A
Asparagales2018-11-02 08:09:53
linux
Asparagales, 2018-11-02 08:09:53

How to customize the bash prompt?

I want to customize the bash prompt to my taste and color. For a normal user, I added the following line to the ~/.bashrc file:
PS1="\[\033[1;34m\][\[email protected]\h] \W \$\[\033[0m\] "
The result is what I wanted: the prompt line is blue and looks something like this:
~ $
I'm trying to achieve the same effect for the root user, only the prompt should be red. To do this, I made the following entry in the /root/.profile file:
PS1="\[\033[0;31m\][\[email protected]\h] \W \$\[\033[0m\] "
This brought the desired result, but not completely. The \$ character in the entry is responsible for displaying the $ character at the input prompt for the normal user and the # character for root. But I have the $ symbol displayed for root too. Is there a possibility to fix this?
Addition. The above result occurs if I switch to the root account in bash using the su - command. If you use just su, then there is no result at all. The /root/.profile file does not work.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
CityCat4, 2018-11-02
@CityCat4

Well, to start with, bash is not guaranteed to work with .profile at all - unless you add it yourself. By default, bash reads .bash_profile first and then .bashrc. The .profile file is read when you run /bin/sh.
PS1 settings are best done not in .bash_profile (which is read once during login), but in .bashrc (which is read during any interactive session). Why? PS1 can easily be broken for example by mc, who tends to break it with his subshell :)
Part of /root/.bashrc

mch=`hostname -s`
PS1='\[\033[01;32m\]${mch:q}:\[\033[01;34m\][$USER]\[\033[01;36m\] \! \[\033[01;31m\]#\[\033[00;38m\] '

S
Saboteur, 2018-11-02
@saboteur_kiev

So actually "-" in the su command means - to read the user's profile, otherwise you simply inherit the rights of root without executing his profile.
And so - still much depends on the distribution kit.
Check the global settings in /etc/profile.d - in RedHat there might be for example RootPrompt.sh which runs as root and overrides your settings.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question