1821051 Members
2763 Online
109631 Solutions
New Discussion юеВ

Variable comparison

 
SOLVED
Go to solution
Yeo Khian Wei
Occasional Advisor

Variable comparison

Hi
In my script I have 2 variables which I want to compare against one another.
a="printer01-01 Apr 10:05"
b="printer01-01 Apr 10:05"

What should the synthax be when i wanna equate whether these 2 are equal to each other?
Should it be $a == $b or wat? Please advise. Thanks in advance.
14 REPLIES 14
Jeroen Peereboom
Honored Contributor
Solution

Re: Variable comparison

A possibility:

if [ "$a" == "$b" ]
then
echo equal
else
echo unequal
fi

Quotes are essential.

Or
if [[ $a == $b ]]
No quotes needed!

JP.
Yeo Khian Wei
Occasional Advisor

Re: Variable comparison

Hi
I have tried as per suggested. But I'm still facing problem. When I tried the first suggestion,

if ["$a"=="$b"]
then
echo equal
else
echo unequal
fi

I got this error:
./test.sh[4]: [printer01-01 Apr 10:05==printer01-01 Apr 10:05]: not found.

Am I doing it correctly?
Michael Schulte zur Sur
Honored Contributor

Re: Variable comparison

Hi,

there must be s space to separate the brackets from the stuff within.
[ "$a" == "$b" ]

hth,

Michael
Jdamian
Respected Contributor

Re: Variable comparison

The error message is due to double equal sign... Just one =, not ==.
The right command line is:

if [ "$a" = "$b" ]
Sanjay Kumar Suri
Honored Contributor

Re: Variable comparison

Following works:

a="printer01-01 Apr 10:05"
b="printer01-01 Apr 10:05"
c="printer01-02 Apr 10:05"

if test "$a" = "$b"
then
echo "equal"
else
echo "unequal"
fi

if [ "$a" = "$c" ]
then
echo "equal"
else
echo "unequal"
fi

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Mark Grant
Honored Contributor

Re: Variable comparison

Actually, it's both the above problems. THe space and the additional "="
Never preceed any demonstration with anything more predictive than "watch this"
KapilRaj
Honored Contributor

Re: Variable comparison

If you can use the Korn Shell (ksh), the following works for me ..

if [ "$a" = "$b" ]
then
echo blabla
fi

Kaps
Nothing is impossible
Jeroen Peereboom
Honored Contributor

Re: Variable comparison

Hmm,

It ('==') behaved well on my Linux / bash. I'll have to check it (for myself). To be honest, I normally use one '=', but all these language differences...

The space it there. It's funny: when I type my answers I have a mono-spaced font, and the spaces are clearly visible. But after posting the reply, the font is variable width and spaces are difficult to see.
That's why you should use Copy/Paste.

Sorry for the confusion.

JP.
Sathish C
Frequent Advisor

Re: Variable comparison

HI
the better way to compare this if u r using kshell is

if [ "${a}" = "${b} ]
then
echo ${a} and ${b} are equal
else
echo ${a} and ${b} are not equal
fi
Some cause happiness wherever they go; others, whenever they go
Elmar P. Kolkman
Honored Contributor

Re: Variable comparison

The problem is not only the spaces between the '[', '"' and the ']', but also the space between the '"' and the '='. It should be:
if["$a"="$b"]

Every problem has at least one solution. Only some solutions are harder to find.
Chia-Wei
Advisor

Re: Variable comparison

You can also try if
[ "$a" -eq "$b" ]
to avoid the space or no space confusion.
Jeroen Peereboom
Honored Contributor

Re: Variable comparison

'-eq' is for integer comparison...

JP.

P.S. I think this thread could be considered closed?
Yeo Khian Wei
Occasional Advisor

Re: Variable comparison

Thanks for all the help.
Finally I'm able to test out my scripts now.

Regards
Khian Wei
KapilRaj
Honored Contributor

Re: Variable comparison