Command to convert an upper-case string to lower-case?

Solution 1:

If the string is already stored in a variable you can use bash's parameter expansion, specifially ${parameter,,pattern} (available since bash 4.0), where parameter is the name of your variable and pattern is ommitted:

$ string="Hello, World!"
$ echo $string
Hello, World!
$ echo ${string,,}
hello, world!

Note that this does not change the value of the variable, only the output. To change the variable you have to assign the new value:

$ echo $string
Hello, World!
$ string=${string,,}
$ echo $string
hello, world!

The upper-case conversion works with ${parameter^^pattern}:

$ echo ${string^^}
HELLO, WORLD!

This works also with Unicode strings (at least with current bash versions, probably needs at least bash 4.3):

$ string='ἈΛΦΆβητος'
$ echo ${string,,}
ἀλφάβητος
$ echo ${string^^}
ἈΛΦΆΒΗΤΟΣ

If you are using zsh, you can use Parameter Expansion Flags (${(FLAGS)NAME}; available since zsh 2.5) to achieve the same results. The bash syntax does not work in zsh 1). The flag for lower case is L; for upper case it is U:

$ string="Hello, World!"
$ echo ${(L)string}
hello, world!
$ echo ${(U)string}
HELLO, WORLD!
$ echo $string
Hello, World!"

This also works with Unicode strings (at least since zsh 5.0; I did not try with earlier versions):

$ string='ἈΛΦΆβητος'
$ echo ${(L)string} 
ἀλφάβητος
$ echo ${(U)string}  
ἈΛΦΆΒΗΤΟΣ



1) Although, seeing that zsh had this for far longer, it should probably be: "the zsh syntax does not work in bash.

Solution 2:

There are very few methods that work correctly with Unicode:

GNU sed 4.2.2 works:

$ echo "Idą gęsi łąką" | sed 's/.*/\U&/'
IDĄ GĘSI ŁĄKĄ

bash 4.2.45 declare does not work:

$ typeset -u ucase; ucase="Idą gęsi łąką"; echo $ucase
IDą GęSI łąKą

bash 4.2.45 parameter expansion does not work:

$ str="Idą gęsi łąką"; echo ${str^^}
IDą GęSI łąKą

bash 4.3.42 declare and parameter expansion work:

$ declare -u ucase
$ ucase="Idą gęsi łąką"
$ echo $ucase
IDĄ GĘSI ŁĄKĄ
$ echo ${ucase,,}
idą gęsi łąką

GNU tr 8.20 does not work:

$ echo "Idą gęsi łąką" | tr '[:lower:]' '[:upper:]'
IDą GęSI łąKą

mawk (default awk in Ubuntu 13.10) does not work:

$ echo "Idą gęsi łąką" | mawk '{print toupper($0)}'
IDą GęSI łąKą

gawk works:

$ echo "Idą gęsi łąką" | gawk '{print toupper($0)}'
IDĄ GĘSI ŁĄKĄ

Perl pure uc() does not work:

$ echo "Idą gęsi łąką" | perl -ne 'print uc($_);'
IDą GęSI łąKą

Python 2 without any Unicode hints does not work:

$ echo "Idą gęsi łąką" | python -c 'import sys; print sys.stdin.read().upper(),'
IDą GęSI łąKą

Python 2 when instructed to deal with Unicode works:

$ echo "Idą gęsi łąką" | python -c 'import sys; print sys.stdin.read().decode("utf-8").upper(),'
IDĄ GĘSI ŁĄKĄ

Python 3 works:

$ echo "Idą gęsi łąką" | python3 -c 'import sys; print(sys.stdin.read().upper(), end="")'
IDĄ GĘSI ŁĄKĄ