1

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?

3
  • Which Unix are you using? Feb 15, 2011 at 10:44
  • ^^^ What he said
    – Linker3000
    Feb 15, 2011 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. Feb 15, 2011 at 18:21

2 Answers 2

1

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

5
  • i initialised it with the one u wrote-- var=$(date -d"-1day" +"%b %d") ...it didnt work...
    – sam
    Feb 15, 2011 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. Feb 15, 2011 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, 2011 at 10:49
  • its not working(either option) . it says date --d : illegal option
    – sam
    Feb 15, 2011 at 11:01
  • It should be a single dash before the d. Works for me (Ubuntu)
    – Linker3000
    Feb 15, 2011 at 11:23
0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .