7

Okay, this is probably a very basic question; but, I'm just getting back in the saddle with Linux.

I have a variable that hold an Epoch time called pauseTime. I need that variable to become human readable (something like 2012-06-13 13:48:30).

I know I can just type in

date -d @133986838 //just a random number there

and that will print something similar. But I need to get the variable to hold that human readable date, instead of the epoch time... I keep running into errors with everything I'm trying. Any thoughts on how I can do this?

w3bguy
  • 2,215
  • 1
  • 19
  • 34

1 Answers1

9

Well do this:

VARIABLENAME=$(date -d @133986838)

and then

export VARIABLENAME

or in Bash do directly:

export VARIABLENAME=$(date -d @133986838)

If you want formatting, say in the usual ISO format:

export ISODATE=$(date -d @133986838 +"%Y-%m-%d %H:%M:%S")
# or
EPOCHDATE=133986838
export ISODATE=$(date -d @$EPOCHDATE +"%Y-%m-%d %H:%M:%S")

The formats behind the + are explained in man date.

Note: $() is the modern form of the backticks. I.e. catching output from a command into a variable. However, $() makes some things easier, most notably escaping rules inside of it. So you should always prefer it over backticks if your shell understands it.

0xC0000022L
  • 20,597
  • 9
  • 86
  • 152
  • Thanks, I had tried something similar... But, following yours I realized that I never cleared out the variable at the end of my loop. Causing it to error out on the next pass. Thanks, that solves my problem. – w3bguy Jun 13 '12 at 19:25
  • 1
    why are you recommending that he export the variable? – jordanm Jun 13 '12 at 22:21
  • Why are you exporting the variable. I'm almost certain it's not necessary. Does a child process need to see it? That would be the only reason to do so. Some people do this habitually and they shouldn't. – Dennis Williamson Jun 14 '12 at 00:39
  • 1
    @jordanm: easy, because this is more versatile. I don't want that he ends up expecting the variable to be usable from some other context of which nothing is mentioned in the question. Sure, it's optional. But without more context I stand by my decision to recommend the `export` :) – 0xC0000022L Jun 17 '12 at 11:02
  • 1
    @jordanm: very constructive. Way to go! You have asked me why I did it, I justified. You reply by downvoting without even a hint why you consider something "bad habit" that is common use everywhere. And if you haven't noticed the first code snippet is entirely without `export` (and that was also for a reason). But sure, have it your way. – 0xC0000022L Jun 17 '12 at 19:16