What is the Bash command I can use to convert an upper-case string to lower-case and vice-versa?
If the string is already stored in a variable you can use
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:
The upper-case conversion works with
This works also with Unicode strings (at least with current bash versions, probably needs at least bash 4.3):
If you are using
This also works with Unicode strings (at least since zsh 5.0; I did not try with earlier versions):
1) Although, seeing that zsh had this for far longer, it should probably be: "the
There are very few methods that work correctly with Unicode:
GNU sed 4.2.2 works:
bash 4.2.45 declare does not work:
bash 4.2.45 parameter expansion does not work:
GNU tr 8.20 does not work:
mawk (default awk in Ubuntu 13.10) does not work:
Perl pure uc() does not work:
Python 2 without any Unicode hints does not work:
Python 2 when instructed to deal with Unicode works:
Python 3 works:
I would use the bash internal typeset or declare command to define a lowercase variable.
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.
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.
I myself still use the typeset syntax as I work on heterogeneous environment, so I have not to rewrite my scripts.
You can do it with the
In the terminal (Ctrl+Alt+T)
will convert lowercase to uppercase.
will convert uppercase to lowercase.
Credit goes to cybercity
You can pipe things through
It's not a pure bash solution, but you can pipe your strings through
Use this simple command For upper to lower case Where 'f' is file name where you want to do conversion.
For lower to upper case