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

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.





Jay is founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or drawing on tablet devices.

He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.

You can leave a comment on my original post.