Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to initialise a variable with yesterday's date in a specific format, eg: If today is 15 Feb, then variable should be var=Feb 14.

date -d"-1day" +"%b %d" is not working, I do not know why.

How do we do it correctly or any other way?

share|improve this question
    
Which Unix are you using? –  Dennis Williamson Feb 15 '11 at 10:44
    
^^^ What he said –  Linker3000 Feb 15 '11 at 11:23
    
as others have said, you need to say which UNIX, or more important, which date command you have. The answers given should work for gnu date. –  Rich Homolka Feb 15 '11 at 18:21

2 Answers 2

Assuming you are using bash:

var=`date -d"-1day" +"%b %d"`

You forgot the backticks. More visual:

var=$(date -d"-1day" +"%b %d")

Read more on Command Substitution

share|improve this answer
    
i initialised it with the one u wrote-- var=$(date -d"-1day" +"%b %d") ...it didnt work... –  sam Feb 15 '11 at 10:39
    
how did you do it? on what system? are you using bash? when i copy paste this exact line it works. How do you check var? when using a variable you have to call it as $var. –  matthias krull Feb 15 '11 at 10:46
    
i am using putty, yes using bash. i knw we use $var. it displays the error message-> date --d : illegal operation –  sam Feb 15 '11 at 10:49
    
its not working(either option) . it says date --d : illegal option –  sam Feb 15 '11 at 11:01
    
It should be a single dash before the d. Works for me (Ubuntu) –  Linker3000 Feb 15 '11 at 11:23

I use:

YESTERDAY=$(date -d yesterday '+%m/%d/%Y')

But this only works for GNU date. Please address the comments on your question, your answer depends on your version of UNIX and the tools.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.