I have a variable of $i which is seconds in a shell script, and I am trying to convert it to 24 HOUR HH:MM:SS. Is this possible in shell?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here’s a fun hacky way to do exactly what you are looking for =)
Explanation:
dateutility allows you to specify a time, from string, in seconds since 1970-01-01 00:00:00 UTC, and output it in whatever format you specify.-uoption is to display UTC time, so it doesn’t factor in timezone offsets (since start time from 1970 is in UTC)date-specific (Linux):-dpart tellsdateto accept the time information from string instead of usingnow@${i}part is how you telldatethat$iis in seconds+"%T"is for formatting your output. From theman datepage:%T time; same as %H:%M:%S. Since we only care about theHH:MM:SSpart, this fits!