You can get the current date in a Unix or Linux shell script using the date
command. To print the current date, you can use the following syntax:
echo $(date)
This will print the date and time in the format Thu Feb 10 16:47:41 PST 2022
.
If you want to display only the date and not the time, you can use the following syntax:
echo $(date +%F)
This will print the date in the format 2022-02-10
.
You can use the +
option to specify the format of the date output. For a full list of format codes, see the man page for the date
command by typing man date
in a terminal window.
Note that the date
command outputs the date and time in the local time zone. If you need to display the date and time in a different time zone, you can set the TZ
environment variable to the desired time zone before calling the date
command. For example:
TZ=America/Los_Angeles
echo $(date)
This will display the date and time in the Pacific Time zone.