Current Year

To get current year in Bash script, use date command with the format that outputs only year.

There are two different types of format in which we could get year from the date. They are

  • Complete Year (Ex: 2021)
  • Last two digits of Year (Ex: 21)

In this tutorial, we will learn the syntax to get the current year in the above said formats, with examples.

Syntax

The syntax of date command to get current year is

</>
Copy
date +%Y

This command outputs the year in the format YYYY.

To get the year in the format yy, just the last two digits, use the following syntax.

</>
Copy
date +%y

Please observe the difference between the case of letter y in the above two expressions.

Examples

Current Year – YYYY

In the following script, we get current year using date command and store it in variable year, and echo it to the standard console.

Example.sh

</>
Copy
year=$(date +%Y)
echo "Current Year : $year"

Output

Current Year : 2021

Current Year – yy

In the following script, we get current year in the format yy, just the last two digits using date command.

Example.sh

</>
Copy
year=$(date +%y)
echo "Current Year : $year"

Output

Current Year : 21

Conclusion

In this Bash Tutorial, we have learnt how to get current year in Bash Script using date command.