There is something I do not understand regarding Bash variable assignment. I want to create a predicate (so, a variable whose value is either true
or false
) foo
that I could use later in my script and that would be equal to the result of a certain Boolean operation. An ideal code would be like the following, which does not work:
var=1
foo=[[ $var = 2 ]] # wishing foo to be equal to the result of the test "$var = 2"
bash: 1 : command not found
foo=test $var = 2
bash: 1 : command not found
In other words, I wonder if there is a shorter way other than:
f(){
if [[ $var = 2 ]]; then echo true
else echo false
fi
}
foo=$(f)
that could give the same final value to foo. What are your thoughts about this? Thank you for your answers.