Bash扩展globbing方括号中断数组initalization

时间:2022-09-01 23:38:52

I used the statement echo *([!min]).css to get all filenames in the current directory with the .css extension, except for the ones with .min.css extension. That worked on the bash.

我使用echo *([!min])语句。使用.css扩展名获取当前目录中的所有文件名,除了使用.min的文件名。css扩展。这在宴会上奏效了。

However, when I use this to initialize an array in a bash script like that

然而,当我使用这个来初始化一个像这样的bash脚本中的数组时

files=(*([!min]).css)

it doesn't work anymore. Bash says there is an unexpected opening bracket somewhere. My editor's syntax highlighting also looks like the brackets of the glob inside the array initialization are not correct, however I wasn't able to get it right.

它不工作了。Bash说,某处有一个意想不到的开头括号。我的编辑器的语法高亮显示也看起来像数组初始化中的glob括号不正确,但是我没能把它写对。

Any advice? Thanks.

任何建议吗?谢谢。

EDIT: I use GNU Bash 4.3.033 on ArchLinux.

编辑:我在ArchLinux上使用GNU Bash 4.3.033。

1 个解决方案

#1


4  

To use extended globs, you must enable the extglob shell option. Put it at the start of your script, just below the shebang:

要使用扩展的globs,必须启用extglob shell选项。把它放在脚本的开头,就在shebang下面:

#!/usr/bin/env bash
shopt -s extglob
#...
files=( !(*.min).css )
#...

Note that shell options are not inherited, so even though you may have extglob enabled in the interactive bash you run the script from, you still have to explicitly enable it in the script.

注意,shell选项不是继承的,因此即使您在运行脚本的交互式bash中启用了extglob,您仍然必须在脚本中显式地启用它。

#1


4  

To use extended globs, you must enable the extglob shell option. Put it at the start of your script, just below the shebang:

要使用扩展的globs,必须启用extglob shell选项。把它放在脚本的开头,就在shebang下面:

#!/usr/bin/env bash
shopt -s extglob
#...
files=( !(*.min).css )
#...

Note that shell options are not inherited, so even though you may have extglob enabled in the interactive bash you run the script from, you still have to explicitly enable it in the script.

注意,shell选项不是继承的,因此即使您在运行脚本的交互式bash中启用了extglob,您仍然必须在脚本中显式地启用它。