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