0

I'm trying to use a variable Timestamp to transfer a date into the following date command:

Timestamp=Sep 17 16:07:21 2014  

Timestampdate=$(date -d $Timestamp +%s)

to get the date in seconds, but I can't get the syntax correct around the $Timestamp variable. Any help please?

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
Andy Kendall
  • 141
  • 1
  • 1
  • 10

1 Answers1

0

You have to surround variables with whitespace characters with quotes to keep them together, or else the shell will interpret the space as a word break, changing your meaning.

Timestamp="Sep 17 16:07:21 2014"
Timestampdate=$(date -d "$Timestamp" +%s)

The auto-splitting of unquoted inserted variables is sometimes useful. For instance:

myargs='-t -r -d'
ls $myargs foo bar baz

gets inserted on the command line literally as ls -t -r -d foo bar baz, in effect allowing a arbitrary arguments to be pulled from a variable.

amphetamachine
  • 27,620
  • 12
  • 60
  • 72
  • Double quotes..... but I was sure I'd tried those too. However you're correct I've tried it and it does work. It's been a long, hot day, and I missed it. Many thanks ALL! – Andy Kendall Sep 18 '14 at 15:46