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
  • 18,825
  • 20
  • 87
  • 132
sansSpoon
  • 1,911
  • 1
  • 21
  • 37

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
  • 713,503
  • 59
  • 514
  • 593