Antonín Kříž Antonín Kříž - 10 months ago 69
Linux Question

Bash - comparing output of two commands

I have this code:


CMDA=$(curl -sI | grep Content-Length)

CMDB=$(curl -sI | grep Content-Length)

if [ "CMDA" == "CMDB" ];then
echo "equal";
echo "not equal";

with this output

root@abcd:/var/www/html# bash
not equal

which should be "equal" instead of "not equal". What did I do wrong?


Answer Source

You forgot the $ for the variables CMDA and CMDB there. This is what you need:

if [ "$CMDA" = "$CMDB" ]; then

I also changed the == operator to =, because man test only mentions =, and not ==.

Also, you have some redundant semicolons. The whole thing a bit cleaner:

if [ "$CMDA" = "$CMDB" ]; then
  echo "equal"
  echo "not equal"