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 bash's parameter expansion, specifially
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
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