在 bash 如何将 extglobed 文件名放入数组?

in bash how to put extglobed file names into array?

我知道我可以 arr=(*.log) 将所有 *.log 文件放入 arr。但是当我尝试使用更复杂的模式的 extglob 时,它似乎失败了:

$ shopt -s nullglob extglob; x=([a-z][0-9].+([0-9]).*.gz); echo "${x}"; shopt -u nullglob extglob;
-bash: syntax error near unexpected token `('

然而没有 extglob 模式 +(match) 它工作:

$ shopt -s nullglob extglob; x=([a-z][0-9].[0-9][0-9].*.gz); echo "${x}"; shopt -u nullglob extglob;
v2.29.2.tar.gz

有什么建议吗?

你必须深入挖掘一下,但是 bash wiki says:

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.

将您的命令序列分成多行,它应该可以工作:

shopt -s nullglob extglob
x=([a-z][0-9].+([0-9]).*.gz)
printf "%s\n" "${x[@]}"
shopt -u nullglob extglob