John W
John W

Reputation: 581

How to find instance of characters in string (not a substring)

I have been looking into this and can't seem to find an answer.

I have a file name stored in string FILENAME

FILENAME="my%fi;le.dat"

I am traversing a directory trying to find files that contain any of these characters:

BAD_CHARS="%^();,"

How can I determine (with bash or tool) if any (1 or more) of those characters are in the filename?

Upvotes: 0

Views: 44

Answers (1)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

With bash's regex matching =~:

$ [[ "$FILENAME" =~ ["$BAD_CHARS"] ]] && echo "bad name!" || echo "valid name"
bad name!

where ["$BAD_CHARS"] forms regex character class [%^();,]

Upvotes: 2

Related Questions