0

I'm trying to write a script that takes a timestamp (just hours, no minutes) written in a file and transforms it in seconds since Epoch, but it is failing and I don't understand why.

The script that I'm using:

#!/bin/bash
time=$(cat time.txt)
stime=$(date --date "$(echo '$time')" +%s)
echo "$stime"

Contents of time.txt

06

Output:

date: invalid date ‘$time’
Bogdan
  • 103
  • 1
  • 12

2 Answers2

4

Because you are single quoting the variable, so it is not being expanded to its contents. Try with double quotes, which allow expansion:

stime=$(date --date "$(echo "$time")" +%s)

BTW, I cannot see any reason to spawn a subshell to echo the variable when you can plainly give the variable to the command:

stime=$(date --date "$time" +%s)
Poshi
  • 5,332
  • 3
  • 15
  • 32
0

On the command

stime=$(date --date "$(echo '$time')" +%s)

You don't need the single quotes on $time. Just use it without them

stime=$(date --date "$(echo $time)" +%s)