Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

date -d"-1day" +"%b %d" is not working. I don't know whether the syntax is incorrect or why else it is not working.

What is the correct way to use it?

share|improve this question

Put $( ) around the command when assigning, eg:


hi=$(date -d'-1day' +'%b %d')

echo "$hi"


Updated as per geirha's comment.

share|improve this answer
it does not work. it says date: --d : illegeal option. i am using putty – user10631 Feb 15 '11 at 11:00
What is the exact way you are using it then? (Update your original post) – Ward Muylaert Feb 15 '11 at 11:04
var=$(date -d"-1day" +"%b %d"); echo $var – user10631 Feb 15 '11 at 11:06
That usage works fine for me. – Ward Muylaert Feb 15 '11 at 11:08
but it shows the message-> date: --d : illegeal option...any other way of doing it? – user10631 Feb 15 '11 at 11:09

Your Answer


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