Samuel Sieb wrote:
On 2021-06-04 1:00 p.m., Patrick Dupre wrote:
> How can I leave this mode extglob ?
Do you need to? It will only apply to the rest of the script anyway.
True.
Though depending on the size of the script and what else it
does, there are certainly times when you don't want this
enabled for the entire script.
There are a few ways to handle that.
You can simply unset it via `shopt -u extglob` after the rm.
Another common method to set an option and only have it
effect a small portion of the code is to use a subshell.
Like:
#!/bin/bash
# some code
(
shopt -s extglob
rm -v !(...)
)
# some other code
The subshell ensures that the shopt is only in effect for
the commands run within the opening and closing parentheses.
There are, as alway, pros and cons to each method. With the
subshell method, a potential con is that the environment
might differ from the rest of the script. If you depend on
variables which are set earlier in the script you just need
to be sure they're available to you in the subshell.
Similarly, if you set a variable in the subshell it won't be
available outside of the subshell.
--
Todd