Chris
Chris

Reputation: 1229

Bash date case not correct

I am using the following in a bash script

echo $(date +'%Y/%b')

I expect "2018/may" but what I actually get is "2018/May". Is there anything I can do to make sure the month is lowercase? I am running in a BSD jail.

Upvotes: 2

Views: 275

Answers (3)

ghoti
ghoti

Reputation: 46856

The command in your question is identical to simply:

date +'%Y/%b'

The date command itself has no options for lower case dates (man strftime for details), but you can force case within bash using parameter expansion:

$ x="$(date '+%Y/%b')"
$ x="${x,,}"
$ printf '%s\n' "$x"
2008/may

Upvotes: 2

Kusalananda
Kusalananda

Reputation: 15633

The month is spelled with a capital first letter in English, that's why there is no format string to get the month in lower case.

In bash, using a lower-case-typed variable:

typeset -l now
now=$(date +'%Y/%b')
printf 'Date is %s\n' "$now"

This will output

Date is 2018/may

Upvotes: 2

Attersson
Attersson

Reputation: 4866

As man date does not show help about lowercase month, I would just pipe it

echo $(date +'%Y/%b') | tr '[:upper:]' '[:lower:]'

Upvotes: 2

Related Questions