What is the syntax to get the current month name (e.g. jan or feb) in a bash script?

  • 6
    See man date for all formatting options. – airhuff Jan 31 '17 at 7:54

You can use the date(1) command.

For example:

date +%b
  • the output is locale-specific. for example, this will return "1月" if your system locale is set to Japanese, use currentmonthname=$(LANG=en_us_88591; date "+%B"); to get English month name – computingfreak Jan 9 at 8:42

The strftime(3) manual (man 3 strftime), if installed on your system, will list all formatting strings that may be used with the date utility. The manual of the date command itself may also contain the supported formatting strings.

To use one of them, for example %b ("the locale's abbreviated month name.") or %B ("the locale's full month name"), you use the + option for date:

$ date +"%B"

$ date +"%b"

$ date +"%B is abbreviated as '%b'"
January is abbreviated as 'Jan'

To get a lowercased abbreviated month in bash:

$ m=$( date +"%b" )
$ echo "${m,,}"

The parameter expansion ${parameter,,pattern} will modify the case of $parameter wherever pattern matches. In ${m,,}, the pattern matches everywhere, so the whole string in $m is lower-cased.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.