Interview Prep/Shell Scripting/Arrays, conditionals and loops

How does pattern matching and regex matching work in `[[ ]]`?

IntermediateConceptArrays, conditionals and loops
Answer

== with an unquoted right side is a glob pattern; =~ is an extended regex, with captures in BASH_REMATCH.

[[ $branch == release/* ]] && echo "release branch"
[[ $branch == "release/*" ]]           # quoted: literal string compare

if [[ $tag =~ ^v([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
  major=${BASH_REMATCH[1]} minor=${BASH_REMATCH[2]}
fi

Do not quote the regex: [[ $x =~ "^[0-9]+$" ]] matches the literal characters. For complex regexes, store them in a variable: re='^[a-z0-9-]{3,63}$'; [[ $name =~ $re ]].

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Arrays, conditionals and loops