bug-bash
[Top][All Lists]
Advanced

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

Re: extglob syntax error in function definition


From: Chet Ramey
Subject: Re: extglob syntax error in function definition
Date: Thu, 8 Oct 2015 10:35:53 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:38.0) Gecko/20100101 Thunderbird/38.3.0

On 10/6/15 8:27 AM, bashbug@jonkmans.nl wrote:

> Bash Version: 4.3
> Patch Level: 30
> Release Status: release
> 
> Description:
>       The shell gives a syntax error when defining a function that uses the 
> extended pattern matching operators.

This is fundamental to how the shell works.  The shell always parses a
complete command before executing any of it.  A shell function definition
is a compound command, so the shell parses the entire function definition
at definition time rather than parsing it piece-by-piece on the fly at
execution time.  Since extglob changes parsing behavior to recognize the
extended pattern matching operators -- which are otherwise syntax errors --
it needs to be enabled before parsing the function definition.


>       I would have expected that i could encapsulate the setting of extglob, 
> by using a subshell-like function:
>               shopt -u extglob
>               isnum () ( shopt -s extglob; case "$1" in  [1-9]*([0-9])) 
> return 0 ;; *) return 1 ;; esac; )

It's not reasonable to expect the `shopt -s' to be executed as part of the
function definition, without running the function at all.

If you want to use the extended pattern matching syntax, you need to have
extglob enabled before you try to parse any commands using it.  That's
just, as I said, fundamental.

There is a compile-time option that enables extglob by default.

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]