Wang
Wang

Reputation: 8183

in bash how to put extglobed file names into array?

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?

Upvotes: 0

Views: 96

Answers (1)

Shawn
Shawn

Reputation: 52449

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

Upvotes: 2

Related Questions