Operating System - Linux
1748265 Members
3838 Online
108760 Solutions
New Discussion юеВ

date command output is inconsistent.

 
SOLVED
Go to solution
Raynald Boucher
Super Advisor

date command output is inconsistent.

Hello all,

Output is correct was running date from command line but incorrect when setting an environment variable.

$ date '+%a %b %_d' --date="20 days ago"
Thu Oct 7
$ KEEP_STRING=`date '+%a %b %_d' --date="
20 days ago"`
$ echo $KEEP_STRING
Thu Oct 7

Can anyone tell me why?
and how to correct it?

Thanks

RayB
4 REPLIES 4
Patrick Wallek
Honored Contributor
Solution

Re: date command output is inconsistent.

Do an:

echo "$KEEP_STRING"

and I think you will see what you are looking for. Without the quotes, all spaces are compressed to a single space.

$ KEEP=$(date '+%a %b %_d' --date="20 days ago")
$ echo $KEEP
Thu Oct 7
$ echo "$KEEP"
Thu Oct 7
$
Patrick Wallek
Honored Contributor

Re: date command output is inconsistent.

Well heck. I tried using the 'retain format' and it didn't help much.

Consider the '_' characters as spaces below in my test output.

$ KEEP=$(date '+%a %b %_d' --date="20 days ago")
$ echo $KEEP
Thu_Oct_7
$ echo "$KEEP"
Thu_Oct__7
$
Raynald Boucher
Super Advisor

Re: date command output is inconsistent.

I'll be darned...
Learn something every day don't we.

I swore at that for an hour yesterday.
Thanks much.

Ray
Raynald Boucher
Super Advisor

Re: date command output is inconsistent.

Worst part is that after checking, I don't need to use the space padding functionality; I'll be looking for a zero padded day.

Take care