To get the current date in a Unix or Linux shell script, you can use the date
command.
Here is the basic syntax for using the date
command to get the current date:
dateSource:www.lautturi.com
This will display the current date and time in the default format.
You can use the +
option to specify a custom format for the date and time.
For example, to get the current date in the format YYYY-MM-DD
, you can use the following command:
date +%Y-%m-%d
This will display the current date in the format YYYY-MM-DD
, where %Y
represents the year, %m
represents the month, and %d
represents the day.
You can use other format specifiers to customize the output of the date
command.
For example, to get the current date and time in the format YYYY-MM-DD HH:MM:SS
, you can use the following command:
date +%Y-%m-%d %H:%M:%S
This will display the current date and time in the format YYYY-MM-DD HH:MM:SS
, where %Y
represents the year, %m
represents the month, %d
represents the day, %H
represents the hour, %M
represents the minute, and %S
represents the second.
You can use the date
command in a shell script by enclosing it in backticks (```).
For example:
#!/bin/bash current_date=`date +%Y-%m-%d` echo "The current date is: $current_date"
This script will get the current date in the format YYYY-MM-DD
and store it in the current_date
variable, and then print the value of the current_date
variable to the terminal.