Problem with expr command in shell script

Hi,

I have used expr command to increment the date.

for e.g.,

case 1 :

echo $(date -d $(echo `expr 20010101 + 1`))

it returns

Tue Jan 2 00:00:00 IST 2001

case 2:

echo $(date -d $(echo `expr 20010101 - 1`))

it returns

date: invalid date `20010100'

please suggest me, how to solve the problem.

Thanks and Regards
Nanthagopal A

No need to use expr, try this instead:-

date -d'+1 day' +"%Y%m%d"
20121205
date -d'-1 day' +"%Y%m%d"
20121203

Modify the format as per your requirement.

1 Like

It does so because 00 isn't a valid day. You can't just add 1 and get a sensible answer.

You can try any other date other than 1 with your code. It will work.

 echo $(date -d $(echo `expr 20000102 - 1`))
Sat Jan 1 00:00:00 PST 2000