I am trying to create a timestamp variable in a shell script to make the logging a little easier. I want to create the variable at the beginning of the script and have it print out the current time whenever I issue echo $timestamp. It proving to be more difficult then I thought. Here are some things I've tried:

timestamp="(date +"%T")" echo prints out (date +"%T")

timestamp="$(date +"%T")" echo prints the time when the variable was initialized.

Other things I've tried are just slight variations that didn't work any better. Does anyone know how to accomplish what I'm trying to do?


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:


# Define a timestamp function
timestamp() {
  date +"%T"

# 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

  • 23
    Depending on how you intend to use this, you'll still need to use command substitution: echo "$(timestamp): something happened". – chepner Jun 12 '13 at 13:16
  • 5
    As for formatting, here is a cut-and-dried set of most frequent formats: zxq9.com/archives/795 – zxq9 Nov 8 '14 at 16:38
  • 144
    For me, I wanted date +"%Y-%m-%d_%H-%M-%S" – Kimball Robinson Dec 31 '14 at 17:57
  • 3
    for some reason this didn't give me the timestamp but the current time with ":" in between. – erikbwork Apr 13 '17 at 11:06
  • 4
    I think a lot of people arrive at this question looking for a way to generate a unix timestamp (as I have) and find the answer by dchakarov to be more useful even though this answer better addresses the asker's question. – vastlysuperiorman Jun 16 '17 at 17:48

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/)

  • 5
    But this variable will just hold the value of the time when the variable was initiated, am I right? – lindhe Aug 14 '14 at 23:04
  • 8
    I guess this is getting a lot of upvotes because it answers the title of the question, but it is not answering the question body : D OP wanted to get a different timestamp every time, whereas this will store one for the whole script. – fedorqui Apr 21 '16 at 6:23
  • I came here looking for this exactly. I.e. the proper format string to get a unix timestamp from date. However, I also upvoted the "correct" answer. I wasn't looking for that, but it's a better answer to the original question and it's also really useful to me. – vastlysuperiorman Mar 22 at 17:24

DATE_WITH_TIME=`date "+%Y%m%d-%H%M%S"` #add %3N as we want millisecond too
  • thanks, that's what I wanted : a timestamp as a variable to re-use in my script ! The shell syntax is so confusing. – Poutrathor Feb 1 at 10:25
  • +1 for adding the millisecond part. This echo $(date +"%Y-%m-%dT%T.%3N%z") However I can't get it to work in a Mac terminal. How to do the same in Mac. Thanks – kosgeinsky Mar 18 at 9:18

ISO 8601 format (2018-12-23T12:34:56) is more readable than UNIX timestamp. However on some OSs you cannot have : in the filenames. Therefore I recommend using something like this instead:


You can use the following command to get the timestamp in this format:


This is the format I have seen many applications use. Another nice thing about this is that if your file names start with this, you can sort them alphabetically and they would be sorted by date.

  • Thanks this is great for my version controls script – Andy May 23 '18 at 15:36

Use command substitution:

timestamp=$( date +%T )
  • 3
    This is what I already tried and it only prints out the time when the variable was initialized. – Dan Jun 12 '13 at 13:09
  • 10
    @dan08: That's how variables work. Use a function if you want dynamic output. – choroba Jun 12 '13 at 13:09

I am using ubuntu 14.04.

The correct way in my system should be date +%s.

The output of date +%T is like 12:25:25.


You can use

timestamp=`date --rfc-3339=seconds`

This delivers in the format 2014-02-01 15:12:35-05:00

The back-tick (`) characters will cause what is between them to be evaluated and have the result included in the line. date --help has other options.

  • 1
    As for what time it gets, this should be executed immediately before inclusion in screen output or a log file intended to have the time of the output listed. – Bill Feb 1 '14 at 20:25
  • 2
    this is less than ideal format for a timestamp because of the space in the output. Be sure to quote it "$timestamp" in usage or you will get two params to the command. e.g. touch $timestamp will produce two files. – harschware Apr 7 '17 at 16:12

Recent versions of bash don't require call to the external program date:

printf -v timestamp '%(%T)T'

%(...)T uses the corresponding argument as a UNIX timestamp, and formats it according to the strftime-style format between the parentheses. An argument of -1 corresponds to the current time, and when no ambiguity would occur can be omitted.


srand without a value uses the current timestamp with most Awk implementations.

  • 3
    why not use the simple date command? – caesarsol Feb 7 '17 at 11:37

Your Answer

