[Top][All Lists]

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

Re: ``shopt -s extglob'' and ``function @() { true; }''

From: Chet Ramey
Subject: Re: ``shopt -s extglob'' and ``function @() { true; }''
Date: Fri, 31 Mar 2017 18:00:16 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:45.0) Gecko/20100101 Thunderbird/45.8.0

On 3/31/17 12:00 AM, Clark Wang wrote:
> There is a post on stackoverflow:
> http://stackoverflow.com/questions/43117707/bashs-strange-behavior-on-a-function-named/

> So when |extglob| is on, |@()| is handled as a glob pattern which makes
> sense. But the behavior after |shopt -s nullglob| indicates that the glob
> pattern |@()| is not /filename-expand/ed for |function @()|. This looks
> kind of counter-intuitive to me.

A shell function name is a WORD, though Posix mode restricts it to a NAME.
It does not undergo any expansions.

When extglob is enabled, @() is part of a WORD, since it's a valid extended
glob pattern, and glob patterns can be contained in a WORD.  This is one
example of the differing parser behavior when extglob is enabled. So
there's no problem with defining a function named `@()'.  When extglob is
not enabled, `@()' is not a valid pattern, and the parens are parsed as
separate operators. In this case they happen to make sense, and define a
function named `@'.

When you try to execute it, since it's not quoted, it undergoes the normal
word expansions, one of which is filename generation.  It doesn't match
anything, so it's left intact, and the function named `@()' executes.  When
nullglob is enabled, the pattern still doesn't match anything, and is
removed. This leaves you with the null command, which also executes, and
returns a status of 0.

``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/

reply via email to

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