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 ]].
More on Arrays, conditionals and loops
How do you declare, populate and iterate an indexed array?BeginnerWhat is the difference between `"${arr[@]}"` and `"${arr[*]}"`?IntermediateHow do associative arrays work and what are they good for in DevOps scripts?IntermediateHow do you read a command's output into an array correctly?IntermediateWhat is the difference between `[ ]`, `[[ ]]` and `(( ))`?BeginnerWhy does `[ $var = "prod" ]` fail when `var` is empty or contains spaces?Intermediate