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
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
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
"..." instead of “...” to quote the parameter expansions.
2014-03-09 != 2014-04-17and when I change the curDate to the same value as nextDate it prints:2014-04-17 = 2014-04-1. Also on ideone: ideone.com/FSFMof and ideone.com/Qu59WJ#!/bin/bashat the top of your script?“and”actually in your script? They should be normal double quotes".