bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: odd behavior from overloading the "source" builtin


From: Chet Ramey
Subject: Re: odd behavior from overloading the "source" builtin
Date: Mon, 17 Mar 2014 09:47:56 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.1.1

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, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]