Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 12 Aug 1998 08:18:51 -0700 (PDT)
From:      patl@phoenix.volant.org
To:        flygt@sr.se
Cc:        FreeBSD Questions <questions@FreeBSD.ORG>
Subject:   Re: BASH prompt question
Message-ID:  <ML-3.3.902935131.4803.patl@asimov>
In-Reply-To: <19980803213618.C11549@sr.se>

next in thread | previous in thread | raw e-mail | index | archive | help
> > > > > I would like to make my bash prompt show a little more info, like
> > > > > what dir the user is in. How would I do this?
> 
> ...
> 
> > PS1='\u@\h:`pwd -P` \$ '
> 
> > \$ will be replaced with uid==0 ? '#' : '$'
> 
> I often find that to be a little too long prompt. If one puts in a \W
> after the h: only the rightmost part of the path is visible.
> Something like: PS1='\u@\h: \W \\$ '

Since I'm usually using some form of X11-based terminal emulator,
I prefer to keep the prompt short and of a (relatively) fixed length.
When using an emulator that supports color, I use:

	: ${USER:=$(/usr/ucb/whoami)}
	export USER
	PS1="\[\033[1;34m\]$USER@\h.\!>\[\033[30m\] "

This will put the login name, hostname, and history entry number
followed by a right broket (greater than sign), in blue.  (I use
red for root.)  Without color support the PS1 line reduces to:

	PS1="$USER@\h.\!> "

I keep track of the path in the window's titlebar.  (This requires
a window manager that let's you change the titlebar text.  Most of
the ones I've tried do; but not all.)  Since I've been doing this
for a while, on a variety of machines and OSes, the code to set it
up is a bit more baroque than strictly necessary:

if  [ "$PS1" != "" ] ; then
    function setup_window_frobbery {
	#   We can't do terminal-specific things when running under the
	#   CDE Display Manager
	if [ -z "$DT" ] ; then
	    # Set the terminal to full eight-bit mode for the Meta-shift keys.
	    # In case we are running in a terminal emulator that loses the
	    # tty settings.  Of course, we lose if the terminal really can't
	    # handle 8bit data; but how often will we be doing that?
	    #
	    if  [ $OS_MAJOR_VERSION = 5 ] ; then
		/bin/stty -markp -parenb -istrip cs8
	    else
		/bin/stty pass8
	    fi
	fi
	

	host=$(uname -n)

	# Stripe has to be a function because it is referenced from
	# functions.  Functions do not perform internal alias
	# substitution.  (It could be a variable, but it might
	# be expanded during function definition instead of during
	# execution, and why clutter the environment with it? )
	if test -s /usr/bin/stripe ; then
	    # Export PWD because without it in the environment
	    # /usr/bin/stripe segfaults.
	    function stripe	{ /usr/bin/stripe $* ; }
	    export PWD
	else
	    function stripe {
		#   I kind of hate to spawn a perl every time this is run;
		#   but there is no builtin way to convert the initial
		#   directory qualifiers back to a tilde when somewhere
		#   under the home directory.
		#
		local Pwd=$(perl -e '$_=$ENV{"PWD"};s:^$ENV{"HOME"}:~:;print')
		#label "Commands:  $USER @ $host - $Pwd"
		label "$USER @ $host - $Pwd"
	    }
	fi

	if  [ `uname -s` = "IRIX" ] ; then
	    # Correct some brain-damaged default key bindings
	    stty intr ^c quit ^\\ 
	fi


	#   Different terminal emulators take slightly different escape
	#   sequences for the title-bar and icon label functions.
	#   Ain't standards wunnerful?
	#
	if [ "$TERM" = "iris-ansi" ] ; then
	    # DCS = ESC P or octal 220
	    # ST  = ESC backslash or octal 234
	    function label	{ builtin echo -ne "\2201.y$*\234" ; }
	    function label-icon	{ builtin echo -ne "\2203.y$*\234" ; }
	elif [ "$TERM" = "xterm" ] ; then
	    #  This sequence changes both the title bar and the icon...
	    function label	{
		builtin echo -ne "\033]0;$*\007"
	    }
	    function label-icon {
		:
	    }
	else
	    #   Sun shelltool/cmdtool, NeWS ANSIterm/jet, etc.
	    #
	    function label	{ builtin echo -ne "\033]l$*\033\\" ; }
	    function label-icon { builtin echo -ne "\033]L$*\033\\" ; }
	    #function label	    {
	    #	perl -e 'print "\033]l'"$*"'\033\\";'
	    #}

	    #function label-icon    {
	    #	perl -e 'print "\033]L'"$*"'\033\\";'
	    #}
	fi


	function cd	    { builtin cd "$@" ; stripe ; }
	function pushd	    { builtin pushd "$@" ; stripe ; }
	function popd	    { builtin popd "$@" ; stripe ; }
	function pwd	    { builtin pwd ; stripe ; }
	function suspend    { builtin suspend ; stripe ; }
	function resize	    { builtin echo -ne "\033[;$1;$2t" ; }
	function su	    {
	    # local IN_SU=$PWD ;
	    # local OLD_HISTFILE=$HISTFILE
	    # unset HISTFILE

	    #   Ensure that the title-bar has some indication that we have
	    #   changed users, even if the new user doesn't update it.
	    label "[ SU $* ] @ $host"

	    #   We want HOME, USER, etc. to be set appropriately for
	    #   root rather than inherited.  Unfortunatly, the Sun
	    #   standard su has no way to do this and still retain
	    #   anything from the old environment (including current
	    #   working directory...)
	    #
	    #   NOTE:   This really should check the parameters to
	    #		determine how to set USER, HOME, etc.
	    #
	    /usr/bin/env - HOME=/ USER=root DISPLAY=$DISPLAY TERM=$TERM
IN_SU=$PWD /usr/bin/su $*
	    
	    # if  [ -n "$HISTFILE" ] ; then
	    #	declare -x HISTFILE=$OLD_HISTFILE
	    # fi
	    
	    stripe
	}

	function rlogin	    {
	    label-icon "[$1]"
	    # /usr/ucb/rlogin $*
	    command rlogin $*
	    stripe ;
	    label-icon "[$host]"
	}
    }


    # The following depends upon the precedence in the expr man page:
    # string-match  ->   * / %   ->  inequalities  ->   &   ->   |
    #
    # WINDOW_PARENT is SunView specific - it isn't there
    # for `openwin -nosunview'.
    #
    # DISPLAY is X11 or X/NeWS specific.
    #
    if [ \(					\
	   \( "$WINDOW_PARENT" \!= "" \) -o	\
	   \( "$DISPLAY"       \!= "" \)	\
          \) -a \(				\
	   \( "$TERM" = "sun"       \) -o	\
	   \( "$TERM" = "sun-cmd"   \) -o	\
	   \( "$TERM" = "xterm"     \) -o	\
	   \( "$TERM" = "dtterm"    \) -o	\
	   \( "$TERM" = "vt100"     \) -o	\
	   \( "$TERM" = "iris-ansi" \) 		\
        \) ]
    then
	#if  tty=$(tty) ; then
	    # Use unique history file for each window.
	    # HISTFILE=$HOME/.dotfiles/.bash_history$DOTHOST.$(tty | cut -d/
-f3)
	    HISTFILE=$HOME/.dotfiles/.bash_history$DOTHOST.$(tty | cut -d/
-f3,4 | tr -d /)
	    if  test "$tty" \!= "/dev/console" ; then
		setup_window_frobbery
	    fi
	#fi
	unset tty
    fi
fi


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?ML-3.3.902935131.4803.patl>