52

What is the Bash command I can use to convert an upper-case string to lower-case and vice-versa?

Thank you.

2

7 Answers 7

65

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.

1
  • 5
    The ${string,,} notation only exists for bash version 4+. Otherwise one of the other solutions is required, such as $(echo $string | tr '[:upper:]' '[:lower:]'). Apr 23, 2016 at 15:34
40

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Ą
5
  • 1
    On Ubuntu 13.04, I have gawk as the default awk (as shown with update-alternatives --display awk). Are you sure mawk is the default in 13.10?
    – kiri
    Dec 27, 2013 at 22:10
  • seds since 1990 have supported the \U option ;-). Not a gnu-sed specific feature. Otherwise, great answer, so many options, so little time ;-) Good luck to all!
    – shellter
    Oct 14, 2014 at 0:28
  • Plus there will be some issues with Turkish (dotted and dotless i), for more look at the following links: - unicode.org/Public/UNIDATA/SpecialCasing.txt - stackoverflow.com/questions/23524231/… - en.wikipedia.org/wiki/Dotted_and_dotless_I
    – azat
    Dec 10, 2014 at 0:07
  • In your example, purse uc doesn't work in Perl because you have to use Encode or use utf8. Rewrite your example as echo "Idą gęsi łąką" | perl -ne 'use Encode;print encode("utf-8",uc(decode("utf-8",$_)))' and it will work. Also see stackoverflow.com/a/13261662/3701431 Jun 9, 2017 at 23:38
  • @shellter – FreeBSD 11.2 sed does not support \U. It gives you a literal U, e.g. UIdą gęsi łąką. Also of note: FreeBSD 11.2 tr works, GNU Coreutils 8.32 tr still fails.
    – Adam Katz
    Oct 8, 2021 at 20:22
21

I would use the bash internal typeset or declare command to define a lowercase variable.

$ typeset -l lcase
$ lcase="LoWeR cAsE"
$ echo $lcase
lower case

The chars are lowered when the value is set, not when you typeset the variable. So it is better to typeset at the beginning of the script. For uppercase you can typeset -u.

$ typeset -u ucase
$ ucase="Upper cAsE"
$ echo $ucase
UPPER CASE

This is not specific to bash shell, that works also on ksh, maybe its in POSIX shell definition.

EDIT: Many people gently pointed me the fact that typeset is now considered obsolete on bash and replaced by declare. Both commands are equivalent.

$ help typeset
typeset: typeset [-aAfFgilrtux] [-p] name[=value] ...
    Set variable values and attributes.

    Obsolete.  See `help declare'.


$ help declare
declare: declare [-aAfFgilrtux] [-p] [name[=value] ...]
    Set variable values and attributes.

I myself still use the typeset syntax as I work on heterogeneous environment, so I have not to rewrite my scripts.

2
  • 2
    +1 Incredible, i never knew that one ! b.t.w. some extra info: I looked this up in the manual and it seems that typeset is considered depricated and replaced by declare
    – thom
    Nov 28, 2013 at 16:15
  • 1
    @thom thank you, it seems that bash started to have a life far from its "bourne shell roots" :)
    – Emmanuel
    Nov 28, 2013 at 16:34
18

You can do it with the tr command.

In the terminal (Ctrl+Alt+T)

This command:

echo 'String here' | tr "[:lower:]" "[:upper:]"

will convert lowercase to uppercase.

echo 'STRING HERE' | tr "[:upper:]" "[:lower:]"

will convert uppercase to lowercase.

Example:

enter image description here

Credit goes to cybercity

2
  • 2
    If you don't care about locales, tr A-Z a-z will work as well. Nov 28, 2013 at 16:47
  • For Unicode, this works in BSD (tested in FreeBSD 11.2) but not in GNU coreutils (8.32). See Michał Šrajer's answer and my comment below it.
    – Adam Katz
    Oct 8, 2021 at 20:27
10

You can pipe things through sed too:

$ echo lowercase | sed 's/.*/\U&/'
LOWERCASE

And \L will have the opposite effect:

$ echo UPPERCASE | sed 's/.*/\L&/'
uppercase
0
1

It's not a pure bash solution, but you can pipe your strings through perl (as Oli did with sed):

$ echo lowercase | perl -ne 'print "\U$_"'
LOWERCASE

And \L will have the opposite effect:

$ echo UPPERCASE | perl -ne 'print "\L$_"'
uppercase
-1

Use this simple command For upper to lower case Where 'f' is file name where you want to do conversion.

tr "A-Z" "a-z" < f

For lower to upper case

tr "a-z" "A-Z" < f
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .