How to: Add or display today's date from a shell script

How to: Add or display today's date from a shell script

To display today's date in a shell script, you can use the date command. By default, date will display the current date and time in the format "Fri Sep 6 10:54:33 PDT 2019".

To specify a different format for the date and time, you can use the -d option and a string that specifies the format. For example, to display the date in the format "YYYY-MM-DD", you can use the following command:

refer to‮:‬lautturi.com
date -d "today" +"%Y-%m-%d"

To add the date to a shell script, you can store the output of the date command in a variable, and then use the variable in your script. For example:

#!/bin/bash

today=$(date -d "today" +"%Y-%m-%d")

echo "Today is $today."

This script will display the current date in the format "YYYY-MM-DD", followed by the text "Today is".

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