Interview Prep/Shell Scripting/Troubleshooting: syntax, parsing and interpreter errors

`[[ $line =~ ^ERROR (.*)$ ]]` fails with `syntax error in conditional expression`.

AdvancedTroubleshootingTroubleshooting: syntax, parsing and interpreter errors
Answer

Likely causes → Unquoted spaces inside the regex split it into separate words that [[ ]] cannot parse. Quoting the whole regex fixes the syntax but makes it a literal string match.

Diagnose → Test the regex alone in a variable and print BASH_REMATCH.

Fix → Store the regex in a variable and expand it unquoted:

re='^ERROR (.*)$'
if [[ $line =~ $re ]]; then msg=${BASH_REMATCH[1]}; fi

Or escape the space: ^ERROR\ (.*)$.

Prevent → Always put non-trivial regexes in a variable; it also makes them readable and reusable.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Troubleshooting: syntax, parsing and interpreter errors