I'm very new to bash and I'm trying to extract a portion of a string based on a pattern, but when I execute my code I'm seeing errors.
Sample code:
#!/bin/sh
STRING="LAX-8912_Words_Are_Here";
if [[ $STRING =~ LAX-(\d)+ ]]; then
echo "${BASH_REMATCH[1]}"
fi
So from the code above, I'm wanting to extract the "LAX-8912" portion of the string. Basically the string will be LAX- and then a series of numbers, could be any length. When the code is exectued however, I'm getting this message:
Syntax error: "(" unexpected (expecting "then")
I've also tried storing the regex in a variable like this:
#!/bin/sh
STRING="LAX-8912_Words_Are_Here";
REX="LAX-(\d)+";
if [[ $STRING =~ $REX ]]; then
echo "${BASH_REMATCH[1]}"
fi
But then I get this error:
[[: not found
My bash version is 4.2.25 so I'm guessing it's not a version issue, but I'm at a bit of a loss as to what's going.