Linux / Unix Shell Script Get Current Year

Linux / Unix Shell Script Get Current Year

To get the current year in a Linux or Unix shell script, you can use the date command to print the current date and time, and then use command substitution to extract the year.

Here is an example of how to get the current year in a shell script:

refer t‮tual:o‬turi.com
#!/bin/sh

# Get the current date and time
current_date_time=$(date)

# Extract the year from the current date and time
current_year=${current_date_time:20:4}

# Print the current year
echo "Current year: $current_year"

This script will print the current year to the terminal. For example, if the current date and time is "Fri Jan 22 14:34:56 EST 2021", the script will print "Current year: 2021".

Keep in mind that the format of the date and time output by the date command may vary depending on your system and the options you pass to the command. You may need to adjust the script to extract the year correctly in your specific environment.

For more information on using the date command and command substitution in shell scripts, you can consult the documentation or seek guidance from the Linux or Unix community.

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