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.
- Obtain the current date and assign it to a variable.
#!/bin/bash
now=$(date +%Y-%m-%d)
echo "当前日期是: $now"
- Output the current date to a file.
#!/bin/bash
now=$(date +%Y-%m-%d)
echo "当前日期是: $now" > date.txt
- 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日"
- 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.