0

I knew I can do arr=(*.log) to get all *.log files into arr. But when I try extglob with more complex pattern it seems fail:

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

however without the extglob pattern +(match) it works:

$ 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

any suggestion?

Wang
  • 7,250
  • 4
  • 35
  • 66
  • It works well on my environment: `bash-4.4.20`. BTW you will need to say `echo "${x[@]}"` to print array elements. `echo "${x}"` just prints `"${x[0]}"`. – tshiono Dec 04 '20 at 12:00

1 Answers1

2

You have to dig for it a bit, but the 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.

Split your sequence of commands up into multiple lines and it should work:

shopt -s nullglob extglob
x=([a-z][0-9].+([0-9]).*.gz)
printf "%s\n" "${x[@]}"
shopt -u nullglob extglob
Shawn
  • 47,241
  • 3
  • 26
  • 60