[nSLUG] "Switchable" bash prompt (change color for root)

InverseCow rowan.townshend at gmail.com
Tue Mar 9 17:37:19 AST 2010


Hi Jack,

  Thanks for your response on this.

  Yes, this would make perfect sense for a standard installation.
  Unfortunately the environment I was trying to get this to work within is
somewhat complex.
  For one, I need to kuu ( http://en.wikipedia.org/wiki/Kerberos_(protocol)
Kerberos ) to get to root.
  I cannot modify anything in /etc/ either given the nature of the
environment (many hosts).
  This lead me to need to fix this via .files inside my home directory.

  I managed to get this working in the end (the BASH part was already
working, but the KSH -> BASH part was not).
  In the end, it seemed that when I kuu'ed to root, it dropped me into KSH
instead of BASH, so my .bashrc was not getting sourced (which makes sense
why my expected prompt was not appearing).
  Once I sorted that, it worked a lot better.

  This should be a lot easier to implement at home where things are not
quite so wonky. :-)

# BASH fix
# NOTE: colors have been "pre-loaded" as variables above with their number
values
# *Shell Settings*
# Define standard user prompt
if [ $( whoami ) == "root" ]
then
  USRCOLOR="$LIGHTRED"
else
  USRCOLOR="$LIGHTPURPLE"
fi

PS1="$DEFAULT[$LIGHTGREEN\t$DEFAULT:$USRCOLOR\u$LIGHTGREY@\h$DEFAULT:$LIGHTCYAN\w$DEFAULT]\n$LIGHTBLUE$
$GREEN"
export PS1


Jack Warkentin wrote:
> 
> Hi Rowan
> 
> I presume that you use su to switch to root.
> 
> Doesn't root's .bashrc get sourced when you do that? Mine certainly 
> does, although I use bash as both my normal user and root user shells.
> 
> You would then just have to set up your PS1 environment variable to 
> whatever you want in root's .bashrc, following Jason's suggestion re 
> colouring.
> 
> Regards
> 
> Jack
> 
> Jason Kenney wrote:
>> Years ago this was part of the default gentoo .bashrc
>> 
>> You can probably find a copy of that floating around if you'd like a
>> more tested implementation, but there would be countless ways to do
>> it:
>> 
>> - man whoami
>> - man id
>> - echo $EUID
>> 
>> There are many tutorials on using ascii coloring in prompts if you're
>> not sure how to do that bit too.
>> 
>> 
>> Jason
>> 
>> On Tue, Mar 9, 2010 at 10:22 AM, InverseCow <rowan.townshend at gmail.com>
>> wrote:
>>> Hi everyone, I would like to add some functionality to my .bashrc at
>>> work,
>>> to differentiate between when I am working as "myself" (uid=94307) and
>>> when
>>> I switch up to "root" (uid=0). Specifically I would like to change the
>>> color
>>> on my prompt to reflect the change. What would be an elegant way to
>>> handle
>>> this? My default shell is KSH (environmental standard), and I switch
>>> over to
>>> BASH before I get a prompt (minor KSH .file hack). I've attached a
>>> hacked
>>> down .bashrc that I use for reference (bashrc_rowan.txt).
>>> "Just because I have a burnt face, doesn't mean I can't fight crime!" --
>>> http://www.burntfaceman.com/
>>> ________________________________
>>> View this message in context: "Switchable" bash prompt (change color for
>>> root)
>>> Sent from the nSLUG mailing list archive at Nabble.com.
>>>
>>> _______________________________________________
>>> nSLUG mailing list
>>> nSLUG at nslug.ns.ca
>>> http://nslug.ns.ca/mailman/listinfo/nslug
>>>
>>>
>> _______________________________________________
>> nSLUG mailing list
>> nSLUG at nslug.ns.ca
>> http://nslug.ns.ca/mailman/listinfo/nslug
>> 
> 
> -- 
> Jack Warkentin, phone 902-404-0457, email jwark at eastlink.ca
> 39 Inverness Avenue, Halifax, Nova Scotia, Canada, B3P 1X6
> _______________________________________________
> nSLUG mailing list
> nSLUG at nslug.ns.ca
> http://nslug.ns.ca/mailman/listinfo/nslug
> 
> 


-----
"Just because I have a burnt face, doesn't mean I can't fight crime!" -- 
http://www.burntfaceman.com/ http://www.burntfaceman.com/ 
-- 
View this message in context: http://old.nabble.com/%22Switchable%22-bash-prompt-%28change-color-for-root%29-tp27836312p27842341.html
Sent from the nSLUG mailing list archive at Nabble.com.




More information about the nSLUG mailing list