Search code examples
bashlowercaseparameter-expansion

How to convert day name to lowercase in bash script?


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.


Solution

  • 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.)