Sunday 6 January 2013

Changing Command Prompt For Root Users

What about root prompt?
 That was a query posted in comments section of an earlier article,  Change Command Prompt. openSUSE does a good job of showing the root prompt in red , to warn it's users that they are in danger of corrupting their system if they are not careful. The default prompt when i change to root user using "su" command is as shown below.
Default Root Prompt
 I am going to answer this from the perspective of openSUSE 12.2 / GNOME 3.4.2 . I open the file "/etc/bash.bashrc" and read the comments section which is as follows:-
# PLEASE DO NOT CHANGE /etc/bash.bashrc There are chances that your changes
# will be lost during system upgrades.  Instead use /etc/bash.bashrc.local 
 I acted upon the advice and create a new file in "/etc" using the command "touch /etc/bash.bashrc.local".
create bash.bashrc.local
 I add the line PS1=$ to this new file and then re-open the command prompt in root mode but the red highlighting is gone. I just see a static dollar '$' prompt.
add PS1 to bash.bashrc.local
 To get the red highlighting back i search for relevant code in "/etc/bash.bashrc". I add  the relevant lines to "/etc/bash.bashrc.local" as shown below and check the prompt again and find that the blood red prompt is back.
_bred="$(path tput bold 2> /dev/null; path tput setaf 1 2> /dev/null)"
_sgr0="$(path tput sgr0 2> /dev/null)"
PS1="\[$_bred\]$\[$_sgr0\]"
unset _bred _sgr0
unset _u _p _t
Customise PS1 variable to show red highlight
 After messing around the code in "/etc/bash.bashrc.local" i brought back the dynamic customised blood red '$' prompt back using the following lines
_bred="$(path tput bold 2> /dev/null; path tput setaf 1 2> /dev/null)"
_sgr0="$(path tput sgr0 2> /dev/null)"
PS1="\[$_bred\]\h:\w $\[$_sgr0\]"
unset _bred _sgr0
unset _u _p _t
Customise  PS1 variable make it dynamic

No comments:

Post a Comment