<HTML ><HEAD ><TITLE >Prompt Depending on Connection Type</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Bash Prompt HOWTO" HREF="index.html"><LINK REL="UP" TITLE="Example Prompts" HREF="c816.html"><LINK REL="PREVIOUS" TITLE='A "Power User" Prompt' HREF="x860.html"><LINK REL="NEXT" TITLE="A Prompt the Width of Your Term" HREF="x869.html"></HEAD ><BODY CLASS="SECT1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Bash Prompt HOWTO: </TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="x860.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 12. Example Prompts</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="x869.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="AEN865" ></A >12.6. Prompt Depending on Connection Type</H1 ><P >Bradley M Alexander (storm@tux.org) had the excellent idea of reminding his users what kind of connection they were using to his machine(s), so he colour-codes prompts dependent on connection type. Here's the bashrc he supplied to me:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="PROGRAMLISTING" > # /etc/bashrc # System wide functions and aliases # Environment stuff goes in /etc/profile # For some unknown reason bash refuses to inherit # PS1 in some circumstances that I can't figure out. # Putting PS1 here ensures that it gets loaded every time. # Set up prompts. Color code them for logins. Red for root, white for # user logins, green for ssh sessions, cyan for telnet, # magenta with red "(ssh)" for ssh + su, magenta for telnet. THIS_TTY=tty`ps aux | grep $$ | grep bash | awk '{ print $7 }'` SESS_SRC=`who | grep $THIS_TTY | awk '{ print $6 }'` SSH_FLAG=0 SSH_IP=`echo $SSH_CLIENT | awk '{ print $1 }'` if [ $SSH_IP ] ; then SSH_FLAG=1 fi SSH2_IP=`echo $SSH2_CLIENT | awk '{ print $1 }'` if [ $SSH2_IP ] ; then SSH_FLAG=1 fi if [ $SSH_FLAG -eq 1 ] ; then CONN=ssh elif [ -z $SESS_SRC ] ; then CONN=lcl elif [ $SESS_SRC = "(:0.0)" -o $SESS_SRC = "" ] ; then CONN=lcl else CONN=tel fi # Okay...Now who we be? if [ `/usr/bin/whoami` = "root" ] ; then USR=priv else USR=nopriv fi #Set some prompts... if [ $CONN = lcl -a $USR = nopriv ] ; then PS1="[\u \W]\\$ " elif [ $CONN = lcl -a $USR = priv ] ; then PS1="\[\033[01;31m\][\w]\\$\[\033[00m\] " elif [ $CONN = tel -a $USR = nopriv ] ; then PS1="\[\033[01;34m\][\u@\h \W]\\$\[\033[00m\] " elif [ $CONN = tel -a $USR = priv ] ; then PS1="\[\033[01;30;45m\][\u@\h \W]\\$\[\033[00m\] " elif [ $CONN = ssh -a $USR = nopriv ] ; then PS1="\[\033[01;32m\][\u@\h \W]\\$\[\033[00m\] " elif [ $CONN = ssh -a $USR = priv ] ; then PS1="\[\033[01;35m\][\u@\h \W]\\$\[\033[00m\] " fi # PS1="[\u@\h \W]\\$ " export PS1 alias which="type -path" alias dir="ls -lF --color" alias dirs="ls -lFS --color" alias h=history </PRE ></FONT ></TD ></TR ></TABLE ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="x860.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="x869.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >A "Power User" Prompt</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="c816.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >A Prompt the Width of Your Term</TD ></TR ></TABLE ></DIV ></BODY ></HTML >