How To: set PS1 to show users "current" directory?

Eric S. Clarke eclarke at ibmpa.awdpa.ibm.com
Wed Apr 17 04:46:45 AEST 1991


In article <1991Apr13.025012.13451 at shared.uucp>, davec at shared.uucp (Dave
Close) writes:
|> In article <1599 at msa3b.UUCP> mgphl at msa3b.UUCP (Michael Phillips)
|> writes:
|> >Trying to set up AIX on a PS/2 & have users that want their command
|> >prompt to show their current directory (aka: $P$G in **DOS).
|> 
|> If you are using an xterm, either SVr4 or aixterm, the following
|> might
|> also be of use.  It doesn't make your prompt so verbose and is
|> always
|> present. 
|> 
|> _cd () {
|>   unalias cd
|>   cd ${@}
|>   alias cd=_cd
|>   if [ "$(whoami)" = "root" ] ; then
|>     pmt=\#
|>   else
|>     pmt=\$
|>   fi
|>   export PS1="^[]0;$(hostname -s):$(whoami)  ${PWD}^G${pmt} "
|> }
|> [ "${TERM}" = "aixterm" -o "${TERM}" = "xterm" ] && {
|>   alias cd=_cd
|>   typeset -fx _cd
|>   if [ "$(whoami)" = "root" ] ; then
|>     pmt=\#
|>   else
|>     pmt=\$
|>   fi
|>   PS1="^[]0;$(hostname -s):$(whoami)  ${PWD}^G${pmt} "
|>   export PS1
|> }
|> 
|> Note, replace the ^[ and ^G with actual escape and bell characters.

Here are a couple of things to consider.  First, since the Korn shell 
reevaluates PS1 every time before displaying a new prompt there is no need to
alias cd.  This can be made as simple as setting the value of PS1.  Since this
is also probably the same env file that gets executed if you use dialup lines,
(That is when X is not useable) check to see if you have an xterm or aixterm.
If you don't set the value of PS1 to display on the command line.  If you do
set similarly to the above example.  There are a few differences that I would
like to point out.

If you change ^[]0 to ^[]1 you can control the icon string and then if you
change it to ^[]2 you can control the title string.  By puting both of these
in your prompt you can seperatly control the name in the title and the icon.

The next issue deals with command line editing in the Korn shell.  The shell
determines where to scroll the command line based on the width of the window
less the number of characters in the prompt.  This means that if you just put
a long PWD path and other misc. stuff in your title bar, the command line
starts to scroll long before it reaches the right margin.  You can fake out the
Korn shell by sending it an ^[[A.  This effectively restarts the count on the
length of the prompt.

So putting it all togeather (Assuming that the various variables are already
set:

case "${TERM}" in
   *xterm)
      export
PS1='^[]1;${HOSTNAME}^G^[]2;${USER}@${HOSTNAME}:${PWD}^G^[[A(!) $ '
      ;;
   *)
      export PS1='${USER}@${HOSTNAME}:${PWD} (!) $ '
esac

When this is displayed in your env file everything past ^[[A will be displayed 
on the next line.  As noted above ^[ = escape and ^G = control G (or Bell).  If
you wish you can change the $ at the end of the prompt to be a variable to 
indicate that the user is the root user.

	
-- 

Eric S. Clarke                     INTERNET:  eclarke at ibmpa.awdpa.ibm.com 
1510 Page Mill Road                    UUCP:  uunet!ibmsupt!eclarke
Palo Alto, CA 94304                IBM VNET:  ECLARKE at AUSVMQ
415-855-4458  T/L: 465-4458            



More information about the Comp.unix.aix mailing list