LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-27-2010, 07:16 PM   #1
tommyttt
Member
 
Registered: Oct 2009
Location: Federal Way, WA
Distribution: openSUSE 11.4 x86_64, openSuSE 12.1, Fedora 15
Posts: 207

Rep: Reputation: 34
Question bash script ? Change PS1 color on per user basis


Been trying to change the color of the shell prompt for each user including root. The things I've tried so far do change the color for each user but don't change the su (root) prompt back to bright red. The su color remains what is set for the user.

The code I've been using is in ~/.bashrc
Code:
### colorize user shell prompt: 2/12/10 TTT
# export PS1="\e[0;36m\u@\h \w> \e[m " # base colored prompt 
# colors:  # replace 0 with 1 for bright color:
#   black  0;30
#   blue   0;34
#   green   0;32
#   cyan   0;36
#   red   0;31
#   purple   0;35
#   brown   0;33

# Change prompt for root
if test "$UID" -eq 0  ; then   <========== this doesn't seem to work
    color="1;31m" # bright red for su(root)
else
    color="0;36m" # color for user
fi
export PS1="\e[$color\u@\h \w> \e[m "
Should this be put somewhere else or is the code wrong?

Admittedly I'm a bash script newbie, normally using c or assembly.

Thanks for any help.
 
Old 02-27-2010, 09:30 PM   #2
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: PCLinux, Slackware
Posts: 8,893

Rep: Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857
Try this:

Quote:
export PS1="\e[0;31m[\u@\h \W]\$ \e[m "
 
Old 02-28-2010, 06:12 PM   #3
tommyttt
Member
 
Registered: Oct 2009
Location: Federal Way, WA
Distribution: openSUSE 11.4 x86_64, openSuSE 12.1, Fedora 15
Posts: 207

Original Poster
Rep: Reputation: 34
Wink

Quote:
Originally Posted by yancek View Post
Try this:
Thanks for the reply Yancek.

That doesn't do what I want. That just changes the "$" for ordinary user with "#" for su(root). The color stays the same. I want to change the color each user selects while retaining the bright red when the user goes su(root).

In unmodified bash prompts, the user prompt is the default color (usually black) while the su(root) prompt changes to bright red.

Your suggestion did give me some ideas about what to research further. Again, thanks.
 
Old 02-28-2010, 11:55 PM   #4
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: PCLinux, Slackware
Posts: 8,893

Rep: Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857Reputation: 1857
I guess I'm not sure what you're trying to do then. I have my user prompt set to brown. When I enter su, enter password and get root prompt it is red. What exactly do you want to change? The entry I posted above was from my user .bashrc, the following, which as you can see is different, is from my root .bashrc:

Quote:
export PS1='\[\033[01;31m\][\u@\h \W]\$\[\033[00m\] '

Don't know if this will help any.
 
Old 03-01-2010, 12:07 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,845

Rep: Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006Reputation: 2006
This is what I use in my bashrc

Code:
export RESET='\e[0m'
export BRED='\e[1;31m'
export BGREEN='\e[1;32m'
export BBLUE='\e[1;34m'
export BMAGENTA='\e[1;35m'

XTITLE='\e]0;\u:\W\a'  # \W is the base directory name.


if [ "$PS1" ]; then
  if [[ "$TERM" =~ xterm ]]; then

    # Set the prompt
    #  Must unquote all the variables.

    if [ "$(id -u)" -eq 0 ]; then
        export PS1='\['${XTITLE}$BRED'\]\u:[\['$MAGENTA'\]$(variprompt)\['$BRED'\]]##\['$RESET'\] '
    else
        export PS1='\['${XTITLE}$BGREEN'\]\u:[\['$BBLUE'\]$(variprompt)\['$BGREEN'\]]$\['$RESET'\] '
    fi

  else

    # Prompt for non xterm consoles--leave out the xterm title setting.

    if [ "$(id -u)" -eq 0 ]; then
      export PS1='\['$BRED'\]\u:[\['$MAGENTA'\]$(variprompt)\['$BRED'\]]##\['$RESET'\] '
    else
      export PS1='\['$BGREEN'\]\u:[\['$BBLUE'\]$(variprompt)\['$BGREEN'\]]$\['$RESET'\] '
    fi

  fi
fi
variprompt is a script I wrote (later converted to a function) that outputs the current directory string based on the current terminal width.

Last edited by David the H.; 03-01-2010 at 12:10 AM. Reason: bunged up the link
 
Old 03-01-2010, 01:12 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,577
Blog Entries: 31

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by tommyttt View Post
The su color remains what is set for the user.
No per-user configuration is done by the su command without the "-" option. If you want per-user configuration to be done when using su, change to su - and su will run the usual shell initialisation, as if the user was logging in.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I change text color in BASH evansd321 Linux - General 1 09-10-2008 05:00 AM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 12:33 PM
Why doesn't the script change PS1? (RedHat) ForumJoiner Linux - Newbie 3 12-01-2006 04:59 AM
how to change user in bash script c0d3 Programming 6 07-27-2005 12:34 PM
help! bash command prompt font/color change richiangpan9 Linux - Newbie 3 04-24-2005 12:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration