0

I have the following in my bash script:

if [ $YEAR_MONTH =~ ^[0-9]{4}-(0[1-9]|1[0-2])$]
        then
                        echo "ERROR: Incorrct folder name format, should be <YYYY-mm>"
                        echo ""
                        print_instructions
                        exit 1
 fi

The regex is good as best as I can tell, but bash throws this error when I try run it:

myuser@myserver:/opt/myfolder$ ./fetch_logs_from_azure.sh list
./fetch_logs_from_azure.sh: line 51: syntax error near unexpected token `('
./fetch_logs_from_azure.sh: line 51: `                if [ $YEAR_MONTH =~ ^[0-9]{4}-(0[1-9]|1[0-2])$]'

I thought I might need to escape the '-' but that didn't make a difference.
What am I doing wrong?

mal
  • 3,022
  • 5
  • 32
  • 62

1 Answers1

1

[ ... ] does not support =~. Use [[ ... ]] instead.

[STEP 101] $ [ $PWD =~ ^[0-9]{4}-(0[1-9]|1[0-2])$ ]
bash: syntax error near unexpected token `('
[STEP 102] $ [[ $PWD =~ ^[0-9]{4}-(0[1-9]|1[0-2])$ ]]
[STEP 103] $
pynexj
  • 19,215
  • 5
  • 38
  • 56