What does the following bash syntax mean:
function use_library {
local name=$1
local enabled=1
[[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && enabled=0
return $enabled
}
I don't particularly understand the line [[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]]
. Is it some kind of regex or string comparison?
This is a trick to compare variables and prevent a weird behaviour if some of them are not defined / are empty.
You can use ,
or any other. The main thing is that it wants to compare ${LIBS_FROM_GIT}
with ${name}
and prevent the case when one of them is empty.
As indicated by Etan Reisner in comments, [[
doesn't have empty variable expansion problems. So this trick is usually used when comparing with a single [
:
This doesn't work:
$ [ $d == $f ] && echo "yes"
bash: [: a: unary operator expected
But it does if we add a string around both variables:
$ [ ,$d, == ,$f, ] && echo "yes"
$
Finally, note you can use directly this:
[[ ,${LIBS_FROM_GIT}, =~ ,${name}, ]] && return 0 || return 1