0

How do I check if the name of some file matches some pattern (has "SSS" in it, for example) in if clause? My variant does not seem to work (it is always false).

for f in B/* ; do
    if [ $f = .*SSS.* ] ; then
#    if [ -f $f ] -and -regex ".*SSS.*"; then
        echo $f
    fi
done
  • Which shell are you using? – Herb Wolfe Feb 26 '17 at 19:24
  • MATE Terminal 1.16.1. – Kosarar Feb 26 '17 at 19:47
  • That's not a shell. I mean like bash, or tcsh or ksh. – Herb Wolfe Feb 26 '17 at 19:48
  • Probably bash. How to come to know? – Kosarar Feb 26 '17 at 19:53
1

You need double brackets, and no '.' (Shell globbing no sed regex)

for f in B/* ; do
    if [[ $f = *SSS* ]] ; then
        echo $f
    fi
done

A bit of google gives you https://stackoverflow.com/questions/2348379/use-regular-expression-in-if-condition-in-bash

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.