Comparing date inside if command in SHELL
With var='date +%d'
you're essentialy assigning the string date +%d
to $var
.
What you probably wanted to do instead, is assign the result of date +%d
to $var
via command substitution:
var=$(date +%d)
if [ $var = 17 ]
then echo yes
else echo no
fi
Three issues:
- You aren't accessing the variable correctly - you need a
$
prefix; - You are using single quotes not backticks, when assigning
var
with the return value of the command; - You are comparing strings (
=
), not integers (-eq
).
var=`date +%d`
if [ $var -eq 17 ]
then echo yes
else echo no
fi