My non-interactive bash shell has extglob off. I would like to turn it on in the statement immediately before a command, but I have noticed that when shopt -s extglob is within an if .. then .. else block, it somehow does not register.
The following extglob-dependant command is not valid: syntax error near unexpected token '('.
Where can extglob be set, and why is there a restriction, at all? Does this apply to other options? … GNU bash 4.1.5
This works:
shopt -s extglob
if true ;then
touch a.bcd; ls <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="afce81ef">[email protected]</a>(bcd)
fi
This fails:
if true ;then
shopt -s extglob
touch a.bcd; ls <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="98f9b6d8">[email protected]</a>(bcd)
fi
... line 17: syntax error near unexpected token `('
... line 17: `touch a.bcd; ls <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="30511e70">[email protected]</a>(bcd)'
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Not sure if there is a more authoritative source (ie. man page / official documentation) on this issue, but I found a site which explains this behavior: http://mywiki.wooledge.org/glob
Because the extglob option changes the way certain characters are parsed, it is necessary to have a newline (not just a semicolon) between the shopt command and any subsequent commands that use extended globs. Likewise, you cannot put shopt -s extglob inside a statement block that uses extended globs, because the block as a whole must be parsed when it’s defined; the shopt command won’t take effect until the block is evaluated, at which point it’s too late. In fact as bash parses the entire statement block before evaluating any of it, you need to set extglob outside of the outermost block.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0