shinzou
shinzou

Reputation: 6192

Does bash search with regex?

Does bash search with regex?

For example, if I wanted to do a for loop that go through all the files of the current directory that start with the letter a, would the following work?

for x in a.*
do

Upvotes: 1

Views: 106

Answers (1)

Eugeniu Rosca
Eugeniu Rosca

Reputation: 5305

When it comes to filename expansion, the ABS states:

Bash itself cannot recognize Regular Expressions.
Inside scripts, it is commands and utilities -- such as sed and awk -- that interpret RE's.

Bash does carry out filename expansion -- a process known as globbing, but this does not use the standard RE set. Instead, globbing recognizes and expands wild cards.

More on wildcards.

However, as mentioned by chepner, bash DOES support regex outside of filename expansion context:

(digit=5; [[ $digit =~ [0-9] ]] && echo match)

Coming back to the OP's question, to loop over all files starting with a:

for x in a* ; do
    [[ -f "$x" ]] || continue
    # process x
done

Upvotes: 5

Related Questions