hc0re
hc0re

Reputation: 1996

How to use DATE command with an external variable?

I'm working on a time convertion script. It is supposed to do something like this:

echo $(( ($(date -d '00:10:2.00' +%s) - $(date -d 0 +%s) ) ))

This line is working just fine giving me the result

602

But I want to put the the first part of the date string (00:10:2.00) under a) an command line argument so it could be read from $1 like:

echo $(( ($(date -d '$1' +%s) - $(date -d 0 +%s) ) ))

OR as a variable:

echo $(( ($(date -d '$myvariable' +%s) - $(date -d 0 +%s) ) ))

When I'm trying this:

foo="00:10:2.00"
echo $foo
echo $(( ($(date -d '$foo' +%s) - $(date -d 0 +%s) ) ))

All I get is:

00:10:2.00
date: invalid date `$foo'
-1417042800

So it's echoing properly but it aint working with the time command...

Upvotes: 0

Views: 59

Answers (1)

Barmar
Barmar

Reputation: 782295

Variables are expanded inside double quotes, they're not expanded inside single quotes. So use

date -d "$1" +%s

Upvotes: 4

Related Questions