[comp.bugs.sys5] ksh bug on function aliasing

schmauss@lan.informatik.tu-muenchen.dbp.de (Oin) (09/04/89)

Here is a bug I've found in ksh @(#)Version 06/03/86.
I declared a function chdir() for the shell builtin cd and
aliased cd to be chdir() ( see script below for details ).
Now have a look at the following two different behaviours of ksh:

Situation 1:
  declare function
  declare alias
  call alias
  everything's fine...


  305 $ chdir ()
  >>> {
  >>> unalias cd
  >>> cd $*
  >>> PS1="${PWD} ! \$ "
  >>> alias cd=chdir
  >>> }
  306 $ alias cd=chdir
  307 $ cd /tmp
  /tmp 308 $ cd /usr/include
  /usr/include 309 $ type cd
  cd is an alias for chdir
  /usr/include 310 $ type chdir
  chdir is a function
  /usr/include 311 $ typeset -f chdir
  function chdir
  {
  unalias cd
  cd $*
  PS1="${PWD} ! \$ "
  alias cd=chdir
  }

Situation 2:
  declare alias
  declare function
  call alias
  @#$%%^&*


  /usr/include 312 $ chdir ()
  >>> {
  >>> unalias cd
  >>> cd $*
  >>> PS1="${PWD} ! \$ "
  >>> PS2="${PWD} >>> "
  >>> alias cd=chdir
  >>> }
  /usr/include 313 $ type cd
  cd is an alias for chdir
  /usr/include 314 $ type chdir
  chdir is a function
  /usr/include 315 $ typeset -f chdir
  function chdir
  {
  unalias cd
  cd $*
  PS1="${PWD} ! \$ "
  PS2="${PWD} >>> "
  alias cd=chdir
  }
  /usr/include 316 $ cd /
  sh: chdir: recursive call



  Franz "Oin" Schmausser            local: lan.informatik.tu-muenchen.dbp.de 
  TU Muenchen, Inst. f. Informatik    DFN: schmauss@{local}                  
  Arcisstrasse                       UUCP: schmauss%{local}@unido.uucp       
  D-8000 Muenchen                 ARPA/CS: schmauss%{local}@relay.cs.net     

allbery@NCoast.ORG (Brandon S. Allbery) (09/06/89)

As quoted from <776@tuminfo1.lan.informatik.tu-muenchen.dbp.de> by schmauss@lan.informatik.tu-muenchen.dbp.de (Oin):
+---------------
| Here is a bug I've found in ksh @(#)Version 06/03/86.
| I declared a function chdir() for the shell builtin cd and
| aliased cd to be chdir() ( see script below for details ).
| Now have a look at the following two different behaviours of ksh:
| 
| Situation 1:
|   declare function
|   declare alias
|   call alias
|   everything's fine...
| 
| Situation 2:
|   declare alias
|   declare function
|   call alias
|   @#$%%^&*
+---------------

This is not a bug; it's documented in the manual.  Since aliases is expanded
at the time that the function definition is read (*not* when it is executed!),
if the alias is defined first you end up with a recursive call.  (And your
"unalias cd" has no effect in *either* version except to blow away your
directory stack routine after the first call.)  If you define the function
first, "cd" isn't an alias when the function is read and the function is
constructed with the built-in "cd", which is what you want.

++Brandon
-- 
Brandon S. Allbery, moderator of comp.sources.misc	     allbery@NCoast.ORG
uunet!hal.cwru.edu!ncoast!allbery		    ncoast!allbery@hal.cwru.edu
"Why do trans-atlantic transfers take so long?"
"Electrons don't swim very fast."  -john@minster.york.ac.uk and whh@PacBell.COM

sft@cbnewsc.ATT.COM (scott.thompson) (09/07/89)

From article <776@tuminfo1.lan.informatik.tu-muenchen.dbp.de>, by schmauss@lan.informatik.tu-muenchen.dbp.de (Oin):
> Here is a bug I've found in ksh @(#)Version 06/03/86.
> I declared a function chdir() for the shell builtin cd and
> aliased cd to be chdir() ( see script below for details ).
> Now have a look at the following two different behaviours of ksh:
> 
 ... example deleted ...
> 

This is not a bug, as a matter of fact it is documented; aliases are
expanded at the time of function DEFINITION.

By the way ksh will expand the $PWD environment variable in PS1
automagically, no need for the function.  I believe this is supported in
the version you are using, just postpone the evaluation of the
appropriate variable, namely $PWD:

			PS1="\${PWD} ! \$ "
			PS2="\${PWD} >>> "

Also, you don't need to unalias cd within the chdir function, just place
it in single quotes.  ie:

			'cd' $*
-- 
  Scott Thompson (IH 6W-207), AT&T Bell Labs, Naperville, Il. 60566

       VOICE: (312)-979-2237     UUCP: ...!att!ihlpa!sft