Ubuntu – Command to convert an upper-case string to lower-case
bashscripts
What is the Bash command I can use to convert an upper-case string to lower-case and vice-versa?
Thank you.
Best Answer
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:
Best Answer
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
.