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? Commented Feb 13, 2020 at 6:11
  • @muru Thanks, I forgot that. Now it works. Commented Feb 13, 2020 at 6:15
  • Can you close this question? Or maybe @muru files an answer Commented Feb 13, 2020 at 7:24

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.