bug-bash
[Top][All Lists]
Advanced

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

Re: shopt can't set extglob in a sub-shell?


From: Chet Ramey
Subject: Re: shopt can't set extglob in a sub-shell?
Date: Sun, 26 Feb 2012 17:54:09 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:8.0) Gecko/20111105 Thunderbird/8.0

On 2/25/12 3:42 PM, Davide Baldini wrote:

> Bash Version: 4.1
> Patch Level: 5
> Release Status: release
> 
> Description:
>       A 'test.sh` script file composed exclusively of the following text
>       fails execution:
>               #!/bin/bash
>               (
>                       shopt -s extglob
>                       echo !(x)
>               )
>       giving the output:
>               $ ./test.sh
>               ./test.sh: line 4: syntax error near unexpected token `('
>               ./test.sh: line 4: `    echo !(x)'
>       Moving the shopt line above the sub-shell parenthesis makes the script
>       work.
> 
>       The debian man pages give no explanation.

Since extglob changes the behavior of the shell parser (for example,
how the parser treats some unescaped parentheses), it has to be enabled
before the commnd containing the extended glob is parsed.  Subshells
with `(', since they are explicitly part of the shell grammar, have
the commands between the parens parsed when the command is read; parsing
of the text enclosed in the parens is not deferred until the subshell.

Putting the shopt in the subshell means that it's not executed before
the extended glob pattern is parsed; moving it outside means that it is.

Chet

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



reply via email to

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