I wrote the following script in Bash but it is not printing the desired output:
a="abc"
if (test "$a"="$a ") then
echo "true "
else
echo "false "
fi
This script should print false but it isn’t. I’m new to Bash scripting, so can anyone please tell me why it is not printing false?
You’re giving
testonly one argument:"$a"="$a ", which is equivalent to"$a=$a ". Whentestgets only one argument, it evaluates to0/true/success if that argument contains at least one character, and to1/false/error otherwise. The minimal fix would be to put spaces around the=, so you’re giving it three separate arguments:but I’d really recommend writing it in a Bashier style:
(Note that the
(and)in the original version merely causetestto be run in a subshell, which really serves no purpose at all.)