2

I'm trying to make a condition that matches any amount of spaces in a file name $f. But what I have seems to be matching everything?

if [[ $f =~ [[:space:]]* ]]; then
    echo found a space
fi

This matches i-have-no-spaces.jpg as well as i have spaces.jpg

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
sansSpoon
  • 2,115
  • 2
  • 24
  • 43

1 Answers1

2

Don't use *, it means 0 or more matches.

Use

if [[ $f =~ [[:space:]] ]]; then
    echo "found a space"
fi

However in BASH, I suggest to not to use regex for this, just use glob matching with =:

if [[ $f = *[[:space:]]* ]]; then
    echo "found a space"
fi
anubhava
  • 761,203
  • 64
  • 569
  • 643