YozSap
YozSap

Reputation: 97

How do I use a variable within the date command in shell?

I've got a cURL command that gets a date and sets it to a variable as a string. But then I want to use that variable as a date so I use the date command on the variable but it seems like the date command doesn't like this. How do I go about setting this variable as a date?

Example:

date=$(curl blahblahblah)
echo $date
"2017-11-02T13:23:52+00:00"
date -d $date +%s
date: invalid date ‘"2017-11-02T13:23:52+00:00"’

When I substitute $date for the actual value, it works fine.

Upvotes: 1

Views: 38

Answers (1)

anubhava
anubhava

Reputation: 784878

Output from curl has double quotes and your date variable has those quotes around the date value.

In bash you can do this to strip double quotes before using date command:

date -d "${date//\"/}" '+%s'
1509629032

If you don't have bash then use:

date -d $(echo "$date" | tr -d '\"') '+%s'
1509629032

Upvotes: 1

Related Questions