Current Month

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

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

  • Month full name (Ex: January)
  • Month short name (Ex: Jan)
  • Month as number (Ex: 01)

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

Syntax

The syntax of date command to get complete name for current month is

date +%B

The syntax of date command to get short name for current month is

date +%b

The syntax of date command to get number for current month is

date +%m
ADVERTISEMENT

Examples

Current Month – Full Name

In the following script, we get current month as complete name of the month using date command, and print it to standard console.

Example.sh

month=$(date +%B)
echo "Current Month : $month"

Output

Current Month : August

Current Month – Short Name

In the following script, we get current month as short name of the month.

Example.sh

month=$(date +%b)
echo "Current Month : $month"

Output

Current Month : Aug

Current Month – Number

In the following script, we get current month as number. If its January month is 01, for February its 02 and so on.

Example.sh

month=$(date +%m)
echo "Current Month : $month"

Output

Current Month : 08

Conclusion

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