cooleck
cooleck

Reputation: 339

Parentheses inside command substitution expression

I need to get array of filenames that have .jpg or .png extensions in my bash script. For that purpose I write this command:

imagesArray=( $(ls (*.png|*.jpg)) )

which supposed to execute ls command with (*.png|*.jpg) argument that defines set of files to output for ls command and then wrap command substitution of ls command in extra parentheses to make array of ls output.

Unfortunately, I get an error: command substitution: line 5: syntax error near unexpected token `(' The problem lies in parentheses of (*.png|*.jpg) expression that are inside the parentheses of command substitution, as far as I know.

Help me with that issue please

Upvotes: 0

Views: 291

Answers (1)

Barmar
Barmar

Reputation: 782693

You don't need to use ls, and shouldn't. See Why not parse ls?

Just put the wildcards directly in the array:

set -o nullglob
imagesArray=(*.png *.jpg)

Setting the nullglob option means the wildcards will expand to empty values if there's no match, rather than expanding to the wildcard itself.

Upvotes: 2

Related Questions