[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
- [Help-bash] Why `echo !(*.l).c` behaves different depending on the context?, Peng Yu, 2019/02/08
- Re: [Help-bash] Why `echo !(*.l).c` behaves different depending on the context?, Eduardo Bustamante, 2019/02/08
- Re: [Help-bash] Why `echo !(*.l).c` behaves different depending on the context?, Peng Yu, 2019/02/09
- Re: [Help-bash] Why `echo !(*.l).c` behaves different depending on the context?, Eduardo Bustamante, 2019/02/09
- Re: [Help-bash] Why `echo !(*.l).c` behaves different depending on the context?, Peng Yu, 2019/02/09
- Re: [Help-bash] Why `echo !(*.l).c` behaves different depending on the context?,
Peng Yu <=
- Re: [Help-bash] Why `echo !(*.l).c` behaves different depending on the context?, Chet Ramey, 2019/02/11
- Re: [Help-bash] Why `echo !(*.l).c` behaves different depending on the context?, Peng Yu, 2019/02/11
- Re: [Help-bash] Why `echo !(*.l).c` behaves different depending on the context?, Chet Ramey, 2019/02/11
- Re: [Help-bash] Why `echo !(*.l).c` behaves different depending on the context?, Peng Yu, 2019/02/11
- Re: [Help-bash] Why `echo !(*.l).c` behaves different depending on the context?, Chet Ramey, 2019/02/11
- Re: [Help-bash] Why `echo !(*.l).c` behaves different depending on the context?, Peng Yu, 2019/02/12