Bash: Display Current Date

ww‮l.w‬autturi.com
Bash: Display Current Date

To display the current date in Bash, you can use the date command. The date command allows you to format the output of the date and time in a variety of ways.

For example, to display the current date in the format YYYY-MM-DD, you can use the following command:

date +%Y-%m-%d

This will output the date in the format YYYY-MM-DD, where YYYY is the four-digit year, MM is the two-digit month, and DD is the two-digit day.

If you want to display the current date and time, you can use the %T format specifier, which will output the time in the format HH:MM:SS, where HH is the hour in 24-hour format, MM is the minutes, and SS is the seconds.

date +%Y-%m-%d %T

This will output the date and time in the format YYYY-MM-DD HH:MM:SS.

The date command offers many more format specifiers that you can use to customize the output of the date and time. You can use the man date command to view the complete list of format specifiers and learn how to use them.

In addition to the date command, you can also use the $(date) syntax to include the current date and time in a string. For example:

echo "The current date and time is $(date)."

This will output the date and time, along with the string that you provide. This can be useful when you want to include the date and time in a message or output from your script.

Created Time:2017-10-16 14:38:39  Author:lautturi