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:
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 zsh1). The flag for lower case is L; for upper case it is U:
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.
$ 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.
If the string is already stored in a variable you can use
bash
's parameter expansion, specifially${parameter,,pattern}
(available since bash 4.0), whereparameter
is the name of your variable andpattern
is ommitted: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
${parameter^^pattern}
:This works also with Unicode strings (at least with current bash versions, probably needs at least bash 4.3):
If you are using
zsh
, you can use Parameter Expansion Flags (${(FLAGS)NAME}
; available since zsh 2.5) to achieve the same results. Thebash
syntax does not work inzsh
1). The flag for lower case isL
; for upper case it isU
: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
zsh
syntax does not work inbash
.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:
bash 4.3.42
declare
and parameter expansion work:GNU tr 8.20 does not work:
mawk (default awk in Ubuntu 13.10) does not work:
gawk works:
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
tr
command.In the terminal (Ctrl+Alt+T)
This command:
will convert lowercase to uppercase.
will convert uppercase to lowercase.
Example:
Credit goes to cybercity
You can pipe things through
sed
too:And
\L
will have the opposite effect:It's not a pure bash solution, but you can pipe your strings through
perl
(as Oli did withsed
):And
\L
will have the opposite effect: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