How to print the current date and time in BASH shell scripts

- by

Sometimes it’s useful to print the current time and date in a BASH script. We can make use of the date command for that. By default, and if called without any parameters, it’ll print something like this:

echo $(date)
Tue 29 Nov 2016 23:08:10 EST

We can shorten this to just the date by using a formatting shortcut like this:

echo $(date +"%x")
29/11/2016

or just the time using this format:

echo $(date +"%r")
11:09:26 pm

Formatting shortcuts can also be used together, like so:

$(date +"%x %r")
29/11/2016 11:03:44 pm

For a complete list of shortcuts, try “man date” from the command line.

Add your voice!

This site uses Akismet to reduce spam. Learn how your comment data is processed.