0

$ date --version date (GNU coreutils) 8.25

When I pass a quoted string into date, the translation to epoch works fine, but when the quoted string is in an environment variable, no joy.

$ date --date "09/25/2016 12:31:52 AM" +%s
1474781512

$ echo $LAT
"09/25/2016 12:31:52 AM"

$ date --date $LAT +%s
date: extra operand ‘AM"’

$ date --date "$LAT" +%s
date: invalid date ‘"09/25/2016 12:31:52 AM"’

$ date --date '$LAT' +%s
date: invalid date ‘$LAT’

What am I doing wrong? (This isn't homework.)

Thanks.

RonJohn
  • 349
  • 8
  • 20

2 Answers2

2

You have literal double quotes around the expanded variable ($LAT) value.

Either remove the double quotes at the time of declaration or use parameter expansion to remove at runtime:

date --date "${LAT//\"/}" '+%s'
heemayl
  • 39,294
  • 7
  • 70
  • 76
  • The (unshown) source of LAT put in the double quotes. Removing them at run-time did the trick. Thanks. – RonJohn Sep 25 '16 at 13:32
0

You have quotes in your variable and that is causing the issues. You can see it here:

$ echo $LAT
"09/25/2016 12:31:52 AM"

If your variable had just the date it wouldn't show the quotes.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74