How to Display the Date and Time in the Linux Terminal (and Use It In Bash Scripts)

Share
  • April 10, 2019

Fatmawati Achmad Zaenuri/Shutterstock.com

The date command is found in the Bash shell, which is the default shell in most Linux distributions and even macOS. This tutorial shows you how to master date on the command line and how you can use it in shell scripts to do more than simply print the time.

Run the date command to see this information. It prints the current date and time for your timezone:

date

Output of the date command

The default formatting looks a little goofy. Why isn’t the year printed after the month and day, instead of being tagged on at the end, behind the timezone? Have no fear: If it’s control over the format of the output you want, date delivers it in spades. There are more than 40 options you can pass to date to instruct it to format its output precisely as you’d like.

To use any of the options type date, a space, a plus sign +, and the option including the leading percentage sign. The %c (data and time in locale format) option causes the date and time to be printed in the normalized format associated with your locale. Your locale is set by the geographical and cultural information you provided when you installed your operating system. The locale governs such things as the currency symbol, paper sizes, timezone, and other cultural norms.

date +%c

Output of the date command with c option

The year now appears in a more natural position in the output.

You can pass several options to date at once. A sequence of options is called a format string. To see the name of the day (%A), the day of the month (%d) and the month name (%B), use this command:

date +%A%d%B

Output of the date command with A d B options

Read the remaining 37 paragraphs

Source : How to Display the Date and Time in the Linux Terminal (and Use It In Bash Scripts)