LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   bash script ? Change PS1 color on per user basis (https://www.linuxquestions.org/questions/linux-software-2/bash-script-change-ps1-color-on-per-user-basis-792036/)

tommyttt 02-27-2010 06:16 PM

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.:Pengy:

yancek 02-27-2010 08:30 PM

Try this:

Quote:

export PS1="\e[0;31m[\u@\h \W]\$ \e[m "

tommyttt 02-28-2010 05:12 PM

Quote:

Originally Posted by yancek (Post 3879530)
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.

yancek 02-28-2010 10:55 PM

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.

David the H. 02-28-2010 11:07 PM

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.

catkin 03-01-2010 12:12 AM

Quote:

Originally Posted by tommyttt (Post 3879457)
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.


All times are GMT -5. The time now is 06:09 PM.