Botacco
Botacco

Reputation: 179

Bash - String comparison does not work

I am trying to compare two variables in a .sh file but it never works ;-(
How can I compare them?

curDate=2014-03-09
nextDate=2014-04-17

if [ “$nextDate” = “$curDate” ]; then
   echo $curDate = $nextDate
else
    echo $curDate != $nextDate
fi

Upvotes: 0

Views: 1522

Answers (2)

Botacco
Botacco

Reputation: 179

I found the solution. In Mac OSX this solution works:

#!/bin/bash
  a=2014-03-09
  b=2014-03-09
if [[ $a == $b ]] ; then
  echo Found.
else
  echo not found
fi

Upvotes: 0

Ivan
Ivan

Reputation: 2320

Everything that is supposed to be a string should better be quoted:

echo "$curDate != $nextDate"

instead of

$curDate != $nextDate

Consider quoting date assignments too.

Try running this:

var=asd das
for thing in $var; do
echo $thing
done

and then with var="asd das"

Asides from that - use " instead of in if [ “$nextDate” = “$curDate” ]; then

Upvotes: 1

Related Questions