How can I use a shell script to get the current year, m…

In a shell script, you can use the date command to retrieve the current year, month, and day. Here are some common ways to obtain the current date.

  1. Obtain the current date and assign it to a variable.
#!/bin/bash
now=$(date +%Y-%m-%d)
echo "当前日期是: $now"
  1. Output the current date to a file.
#!/bin/bash
now=$(date +%Y-%m-%d)
echo "当前日期是: $now" > date.txt
  1. Obtain the current year, month, and day and assign them to variables respectively.
#!/bin/bash
year=$(date +%Y)
month=$(date +%m)
day=$(date +%d)
echo "当前日期是: $year年$month月$day日"
  1. Get the current year, month, and day and output them to a file.
#!/bin/bash
year=$(date +%Y)
month=$(date +%m)
day=$(date +%d)
echo "当前日期是: $year年$month月$day日" > date.txt

Here are some common ways to get the current date, you can choose the method that suits your needs.

bannerAds