Bash really confuses the issue with the likes of [
, [[
, ((
, $((
, etc.
All treatingtreading on each others' code spaces. I guess this is mostly historical, where Bash had to pretend to be sh
occasionally.
Most of the time, I can just pick a method and stick with it. In this instance, I tend to declare (preferably in a common library file I can include with .
in my actual script(s)).
TRUE=1; FALSE=0
I can then use the ((
... ))
arithmetic operator to test thusly.
testvar=$FALSE
if [[ -d ${does_directory_exist} ]]
then
testvar=$TRUE;
fi
if (( testvar == TRUE )); then
# doDo stuff because the directory does exist
fi
You do have to be disciplined. Your
testvar
must either be set to$TRUE
or$FALSE
at all times.In
((
...))
comparators, you don't need the preceding$
, which makes it more readable.I can use
((
...))
because$TRUE=1
and$FALSE=0
, i.e. numeric values.The downside is having to use a
$
occasionally:testvar=$TRUE
which is not so pretty.
It's not a perfect solution, but it covers every case, I need of such a test.