help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Why `echo !(*.l).c` behaves different depending on the c


From: Peng Yu
Subject: Re: [Help-bash] Why `echo !(*.l).c` behaves different depending on the context?
Date: Sat, 9 Feb 2019 07:03:16 -0600

On Sat, Feb 9, 2019 at 12:03 AM Peng Yu <address@hidden> wrote:
>
> On Fri, Feb 8, 2019 at 11:20 PM Eduardo Bustamante <address@hidden> wrote:
> >
> > On Fri, Feb 8, 2019 at 9:05 PM Peng Yu <address@hidden> wrote:
> > (...)
> > > I am not sure this link is relevant.
> >
> > OK, since you didn't read the link:
> >
> > | extglob changes the way certain characters are parsed. It is
> > necessary to have a newline (not just a semicolon) between shopt -s
> > extglob and any subsequent commands to use it. **You cannot enable
> > extended globs
> > | inside a group command that uses them**, because **the entire
> > block** is parsed before the shopt is evaluated. Note that the typical
> > function body is a group command. An unpleasant workaround could be to
> > use a subshell
> > | command list as the function body.
>
> I still get the error when I use subshell. Do I miss something here?
>
> $ cat main.sh
> #!/usr/bin/env bash
> # vim: set noexpandtab tabstop=2:
>
> function f {
>     (
>         shopt -s extglob
>         echo !(*.l).c
>     )
> }
>
> set -v
> f
> $ ./main.sh
> ./main.sh: line 7: syntax error near unexpected token `('
> ./main.sh: line 7: `        echo !(*.l).c'

I solved it by using `eval`. Nevertheless, I don't think this part of
the bash grammar is well designed. But I am not sure if there is an
easy way to improve it in the current parsing framework.

$ cat ./main.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

function f {
        shopt -s extglob
        eval 'echo !(*.l).c'
}

set -v
f
$  ./main.sh
f
echo !(*.l).c
main.c

-- 
Regards,
Peng



reply via email to

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