BladeSystem Forums have moved here
To make BladeSystem information easier to find, we have moved the BladeSystem forums here, to Servers and Operating Systems.
Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

prompt date string with bash

Go to Solution
Occasional Contributor

prompt date string with bash

I have stack with making shell programming. I hope somebody will help me!

Try to prompt date string that shows the date of 2 month ago from today.

I know how to make it on Solaris and Aix.
eg. dst=`env TZ=JST+1440 date +%Y%m%d`
echo $dst
This will prompt like 20050504

However, it doesn't work on HPUX... Please does sombody give me some advice.

Thank you in advance.
Esteemed Contributor

Re: prompt date string with bash


here is one way
echo $(sh -c "TZ=$(date +%Z)+1440; export TZ; date '+%m%d%y'")

where 1440 is the number of hours between two months which I have taken from your example above


Acclaimed Contributor

Re: prompt date string with bash

First of all, the date 2 months ago is a very imprecise designation. For example, what is 2 months before 30-Apr-2005. 30-Feb-2005? I don't think so. If you change your question to something like 60 before today then thats much easier.

Use the attached (or search for; it works just the same).

DT=$( -y -s $( -p 60))
echo "60 days ago was ${DT}"

Invoke as -u for full usage and examples.
If it ain't broke, I can fix that.
Esteemed Contributor

Re: prompt date string with bash

I forgot you wanted it in a script, I gave i for the command line
okay here it is

dst=`sh -c "TZ=$(date +%Z)+1440; export TZ; date '+%m%d%y'")`
echo $dst

Occasional Contributor

Re: prompt date string with bash

Hello Devesh and Clay.

Sorry for replying so late. I appliciate what you have given me advice and tips.

Now I have found why I couldn't get the target date. The reason is that the environmental variable TZ was set to "MST7MDT". I changed it to "JST-9", then the command "env TZ=JST+15 date" works!

Sorry for putting such a dum thread... I'm going to close this thread.

Best regards,


Occasional Contributor

Re: prompt date string with bash


# env | grep TZ

on this setting, a command "env TZ=JST+15 date" does not provide the correct answer. As above command, my timezone is JST so that TZ variable must change to JST. I changed it to JST,


# env | grep TZ
# TZ=JST-9

then, the command works. The command gets yesterday's date.