How to get date-3 value in sh.

Hi,

I am doing a shell script and I need the date (tdate-3) value, example if my tdate is,

tdate=`date +%Y%m%d_%I%M%p` (variable 1 could be from few hours old, not present date at the time I need prevdate value)

prevdate === ? (should variable should be tdate-3)

Please help.

Thanks !

The answer/solution depend on OS. For FreeBSD

# date && date -v -3d
Mon May 10 16:07:23 EDT 2010
Fri May  7 16:07:23 EDT 2010

Thanks for the response.

I am on Ubuntu working on bash.

date && date -v -3d

does d in -3d mean day ? I need say -4hours.

On linux

> date && date -d "3 days ago"
Mon May 10 23:30:14 CEST 2010
Fri May  7 23:30:14 CEST 2010
> date && date -d "3 hours ago"
Mon May 10 23:30:15 CEST 2010
Mon May 10 20:30:15 CEST 2010

---------- Post updated at 05:33 PM ---------- Previous update was at 05:32 PM ----------

Please use [code] tags when you post code or data sample.

Thanks,

I am trying to save this output to a variable, but it throw me an error:

 
#!/bin/sh
cdate=`date +%Y%m%d_%I%M%p`
$pdate=date -d "3 days ago"
echo $cdate
echo $pdate
 
Error message:
: =date: not found

I am sorry if I am asking for the basic, this is my first project at work.

#!/bin/sh
cdate=`date +%Y%m%d_%I%M%p`
pdate=`date -d "3 days ago"`
echo $cdate
echo $pdate
1 Like

Ha :slight_smile:

Thank u so much.

A set of small quotes killed my day !