Using brace expansion in if statement

user14492

Let's say I've got a list with file names in it with date. I want to do something on files with extension png or jpg of a specific date. I've got the following code:

year="2015"
for f in $myFiles
do
    if [[ $f = *"$year"{".png",".jpg"} ]]
    then
        echo $f
    fi
done

This doesn't seem to work, no file passes the condition. I could do it by using two conditions, and using an or condition; but I was wondering what I am doing wrong. Brace expansion should work, otherwise how should I use it.

anubhava

{...} doesn't expand for if condition. You can use extglob:

shopt -s extglob

[[ $f = *"$year".@(png|jpg) ]] && echo "$f"

You may not even need a for loop with extglob, you can just do:

printf "%s\n" *"$year".@(png|jpg)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related