0

Can someone help me understand what's wrong with this script (ran in cygwin)?

$ cat myscript.sh
#!/bin/bash
if [[ $myfile =~ (.+)-(.+)\.(.+) ]]; then
    echo "Prefix = ${BASH_REMATCH[1]}"
    echo "Name = ${BASH_REMATCH[2]}"
    echo "Extension = ${BASH_REMATCH[3]}"
else
    echo "NO MATCH"
fi
$ echo $myfile
prefix-name.ext
$ ./myscript.sh
NO MATCH

If I check the pattern matching (without the IF) everything works fine:

$ [[ $myfile =~ (.+)-(.+)\.(.+) ]]; echo $?
0
$ echo "Prefix = ${BASH_REMATCH[1]}"
Prefix = prefix
$ echo "Name = ${BASH_REMATCH[2]}"
Name = name
$ echo "Extension = ${BASH_REMATCH[3]}"
Extension = ext
3
  • 2
    Did you export myfile? – muru Feb 13 '20 at 6:11
  • @muru Thanks, I forgot that. Now it works. – Jorge Lopez Feb 13 '20 at 6:15
  • Can you close this question? Or maybe @muru files an answer – Jonas Berlin Feb 13 '20 at 7:24

Browse other questions tagged or ask your own question.