Converting a random epoch time into a readable format

I am trying to create a script that will take epoch (input from command line) and convert it into a readable format in bash/shell

---------- Post updated at 08:03 PM ---------- Previous update was at 07:59 PM ----------

#!bin/bash

read -p "Please enter a number to represent epoch time:" STRING
echo "The UNIX time for $STRING is ($(($date -d "$STRING" + %m-%d-%Y %T utc)"

@ feature present since GNU coreutils 5.3.0

date -d@"$STRING"

When I ran the code with the @ symbol - it returns

date: invalid date '@String'

should I rename STRING to something else? or am I missing a parameter with this code? I took a scripting class but it was years ago and I cannot find anything online. :frowning:

Show us what exactly you did?

#!/bin/bash

read -p "Please enter a number to represent epoch time:" STRING
echo "The UNIX time for $STRING is (date -d@$"STRING" +%m-%d-%Y %T -utc)"

Output: Please enter a number to represent epoch time:34565444
The UNIX time for 34565444 is (date -d@$STRING +%m-%d-%Y %T -utc)

$"STRING" is wrong, try "$STRING"

I did that but still received the same output, so:

I tried this:

#!/bin/bash

read -p "Please enter a number to represent epoch time:" STRING
echo "The UNIX time for $STRING is:" $($((date -d@"$STRING" +%m-%d-%Y %T -utc)))

The output was:

Please enter a number to represent epoch time:3456543
./test1.bash:  line 4: date -d@"3456543" +%m-%d-%Y %T -utc: syntax error: invalid  arithmetic operator (error token is "@"3456543" +%m-%d-%Y %T -utc")
The UNIX time for 3456543 is:

Correction:

echo "The UNIX time for $STRING is: $(date -d@$STRING +'%m-%d-%Y %T' --utc)"

Remember, if you're not running an external command, you don't need $( ). And there's usually not much reason to nest them 3 deep.

YES!! It Worked :slight_smile:

Thank you!!