1

I'm trying to understand this behaviour of date in bash script. When I call date by itself and when I format the date I get different outputs.

date
Wed Aug 31 22:12:25 EDT 2016
date --date="$NOW" +"%X %x"
12:00:00 AM 08/31/2016
date --date="$NOW" +"%Y/%m/%d %H:%M:%S"
2016/08/31 00:00:00

As you can see the time portion is zeroed out. Why is formatted date result different than just calling date ?

closed as off-topic by Gilles, Thomas Dickey, Julie Pelletier, Archemar, GAD3R Sep 2 '16 at 11:12

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Gilles, Thomas Dickey, Julie Pelletier, Archemar, GAD3R
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 6
    Is $NOW empty? as noted under DATE STRING in the GNU date manual page, "An empty string indicates the beginning of the day." – steeldriver Sep 1 '16 at 2:31
  • Bingo! $NOW was empty, I've removed it as per @Sato Katsura suggestion and it works! Can you post as an answer for me to accept ? – Fawix Sep 1 '16 at 15:47
2

Remove the $ that is referring to an obviously unset variable and you'll get what you want:

date --date="NOW" +"%Y/%m/%d %H:%M:%S"

The same logical representation can allow you to show the date for tomorrow, next week, +1 week, last week, +1 day, and much more.

  • 2
    Or just leave out --date="NOW" altogether? – Satō Katsura Sep 1 '16 at 5:37
  • Of course, but the point here is to show that it supports logical representation of the date. – Julie Pelletier Sep 1 '16 at 5:48
  • Sorry the $ was actually to show it's a shell entry (not a response). It is not there in my actual command. I'll try without the --date="NOW". I'll also edit the misleading $ out of my question :) – Fawix Sep 1 '16 at 15:41

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