Bash – How to change screen’s hardstatus color based on the logged in user

bashbashrccolorsgnu-screen

I haven't edited/created a .screenrc file in a while, but this is what I'm currently using:

# Turn off the screen startup message
startup_message off

# Define format of bottom navigation/status/date/etc. line
hardstatus alwayslastline "%{yk}[ %H ] %-Lw%50>%{gk}(%{-}%n-%t%{gk})%{-}%+Lw%< %=%{yk}[ %c %d.%m.%Y ]"

# Increase scrollback buffer to 30000 lines
defscrollback 30000

I honestly don't remember what everything in my hardstatus line stands for, but I want to figure out how I could change the hostname color based on the user that is logged in. Is this even possible? Right now, the hostname is yellow when I'm logged in under any user; but I want it to be red if I'm root. Is this something that can be changed in my .bashrc file? I have this in my .bashrc to change new screen window names to the hostname of the server I'm connected to:

# Set screen window title
case "$TERM" in
screen)
  PROMPT_COMMAND='echo -ne "\033k$HOSTNAME\033\\"'
  ;;
esac

Thanks for any help!

EDIT

I've tried adding this to my .bashrc file, but it just puts the ANSI code in-front of the hostname in my screen hardstatus:

case "$TERM" in
screen)
  if (( $UID == 0 )); then
    PROMPT_COMMAND='echo -ne "\033k$FRED$HOSTNAME$RS\033\\"'
  else
    PROMPT_COMMAND='echo -ne "\033k$HOSTNAME\033\\"'
  fi
  ;;
esac

EDIT 2

I've also added the actual ANSI color codes (ex: \[\033[31m\]) and the screen escape codes (ex: %kr) to the above statement in my .bashrc, but neither worked.

EDIT 3

If I use this as the prompt command line, the hostname appears in red in-front of my PS1:

PROMPT_COMMAND='echo -ne "\033[31m$HOSTNAME\033\\"'

If I use the following prompt, the window name changes to the hostname (YES!); but the color remains the default (NO!):

PROMPT_COMMAND='echo -ne "\033[31m\033k$HOSTNAME\033\\"'

I've also tried replacing \033[31m (and just [31m) with the screen color escape (%kr), but that did not work either.

Best Answer

Take a look at this U&L Q&A.

Specifically this answer, https://unix.stackexchange.com/a/16433/7453.

The general idea is that you maintain 2 different screenrc files with the colors and based on your $USER in your bashrc file you point the environment variableSCREENRC to the one for a given user.