How to convert day name to lowercase in bash script?

1k Views Asked by At

I usually use the following bash script to rename file with the days of the week (e.g., Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday):

#get date
export LANG=id_ID
export TZ=Asia/Jakarta
DAY=$(date --date='0 days' '+%A')
TODAY=$(date --date='0 days' '+%Y%m%d')

#get each page 1 till 9
PAGE=1
until [ $PAGE -gt 9 ]; do
mv "0$PAGE".jpg banjarmasinpost"$TODAY"-"$DAY"_"0$PAGE".jpg
let PAGE+=1
done

Is there a way to make all the names of the day lowercase, such as monday, tuesday, wednesday, thursday, friday, saturday, and sunday? Thank for all.

2

There are 2 best solutions below

0
On BEST ANSWER

Bash parameter expansion to the rescue! ${DAY,,} will expand to the lower-cased value of $DAY.

(Also, variable names in shell scripts should be lower case -- see this unix.stackexchange question.)

0
On

See the bash reference manual:

${parameter^pattern}
${parameter^^pattern}
${parameter,pattern}
${parameter,,pattern}

Case modification. This expansion modifies the case of alphabetic characters in parameter. The pattern is expanded to produce a pattern just as in pathname expansion. Each character in the expanded value of parameter is tested against pattern, and, if it matches the pattern, its case is converted. The pattern should not attempt to match more than one character. The ^ operator converts lowercase letters matching pattern to uppercase; the , operator converts matching uppercase letters to lowercase.The ^^ and ,, expansions convert each matched character in the expanded value; the ^ and , expansions match and convert only the first character in the expanded value. If pattern is omitted, it is treated like a ‘?’, which matches every character. [...]

So if $DAY contains Wednesday, you can write ${DAY,,} to get wednesday:

$ DAY=$(date --date='0 days' '+%A')
$ echo $DAY
Wednesday
$ echo ${DAY,,}
wednesday

(You could in this case also use ${DAY,}, since you only need to worry about a single letter.)