Re: odd behavior from overloading the source builtin

2014-03-17 Thread Chet Ramey
On 3/16/14 5:17 PM, Doug McIlroy wrote:

  Apologies for having sent the wrong message before

  GNU bash, version 4.2.39(1)-release (x86_64-redhat-linux-gnu)[5~
  interprets this shell script differently when bash input comes
  from a terminal or when it conmes from a file:
   source(){
   echo x
   }
   source
  In the former case it prints x; in the latter it gives a
  missing-filename diagnostic, presumably from the builtin source.
  The behavior is independent of whether the environment
  contains source().

  The bash man page leads me to expect the former behavior
  because commands are said to be looked up first among
  shell functions, then among builtins.

  The behavior seems to be peculiar to source. If source
  is replaced by wait, the difference vanishes.

This behavior isn't affected by the difference between an interactive and a
non-interactive shell; it's one of the properties of posix mode.

First, a couple of stipulations: `source' is identical to `.', and `.' is
one of the Posix special builtins.

Posix requires that special builtins be searched before shell functions in
the command lookup order, so bash-4.2 allows you to define a shell function
with the same name as a special builtin, but not to call it.

That behavior was called out in a post-Posix 2008 interpretation (383), and
the 2013 version of the standard was changed to make it an application
requirement, but it's pretty clearly up to the shell to enforce it.  I
changed bash-4.3 posix mode to make attempting to define a shell function
with the same name as a special builtin a run-time error, as other shells
(ksh93, dash) do.  This behavior is not universal; other shells (mksh,
posh) don't make it an error at all.

This is documented in the section of the info manual describing posix mode:

 19. Function names may not be the same as one of the POSIX special
 builtins.

 20. POSIX special builtins are found before shell functions during
 command lookup.

The man page contains a URL pointing readers to the POSIX file.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



odd behavior from overloading the source builtin

2014-03-16 Thread Doug McIlroy


binxQRPThwkqX.bin
Description: Binary data


Re: odd behavior from overloading the source builtin

2014-03-16 Thread Chris F.A. Johnson

On Sun, 16 Mar 2014, Doug McIlroy wrote:


GNU bash, version 4.2.39(1)-release (x86_64-redhat-linux-gnu)
interprets this shell script differently when bash input comes 
from a terminal or when it conmes from a file:

source(){
echo x
}
source
In the former case it prints x; in the latter it gives a
missing-filename diagnostic, presumably from the builtin source.
The behavior is independent of whether the environment
contains source().



The bash man page leads me to expect the former behavior
because commands are said to be looked up first among
shell functions, then among builtins.



The behavior seems to be peculiar to source. If source
is replaced by wait, the difference vanishes.


   I get the same behaviour whether entered in a terminal or executed
   in a script (bash 4.2 and 4.3).

$ source(){
  echo x
}
$ source
x
$ xx.sh
x