Create timestamp variable in bash script
If you want to get unix timestamp, then you need to use:
timestamp=$(date +%s)
%T
will give you just the time; same as %H:%M:%S
(via http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/)
In order to get the current timestamp and not the time of when a fixed variable is defined, the trick is to use a function and not a variable:
#!/bin/bash# Define a timestamp functiontimestamp() { date +"%T" # current time}# do something...timestamp # print timestamp# do something else...timestamp # print another timestamp# continue...
If you don't like the format given by the %T
specifier you can combine the other time conversion specifiers accepted by date
. For GNU date
, you can find the complete list of these specifiers in the official documentation here: https://www.gnu.org/software/coreutils/manual/html_node/Time-conversion-specifiers.html#Time-conversion-specifiers