Is there a way in bash to convert a string into a lower case string?
For example, if I have:
a="Hi all"
I want to convert it to:
"hi all"
Is there a way in bash to convert a string into a lower case string?
For example, if I have:
a="Hi all"
I want to convert it to:
"hi all"
In Bash 4:
To lowercase
To uppercase
Toggle (undocumented, but optionally configurable at compile time)
Capitalize (undocumented, but optionally configurable at compile time)
Title case:
To turn off a
declare
attribute, use+
. For example,declare +c string
. This affects subsequent assignments and not the current value.The
declare
options change the attribute of the variable, but not the contents. The reassignments in my examples update the contents to show the changes.Edit:
Added "toggle first character by word" (
${var~}
) as suggested by ghostdog74.Edit: Corrected tilde behavior to match Bash 4.3.
tr:
AWK:
sed:
I know this is an oldish post but I made this answer for another site so I thought I'd post it up here:
UPPER -> lower: use python:
Or Ruby:
Or Perl (probably my favorite):
Or PHP:
Or Awk:
Or Sed:
Or Bash 4:
Or NodeJS if you have it (and are a bit nuts...):
You could also use
dd
(but I wouldn't!):lower -> UPPER:
use python:
Or Ruby:
Or Perl (probably my favorite):
Or PHP:
Or Awk:
Or Sed:
Or Bash 4:
Or NodeJS if you have it (and are a bit nuts...):
You could also use
dd
(but I wouldn't!):Also when you say 'shell' I'm assuming you mean
bash
but if you can usezsh
it's as easy asfor lower case and
for upper case.
If using v4, this is baked-in. If not, here is a simple, widely applicable solution. Other answers (and comments) on this thread were quite helpful in creating the code below.
Notes:
a="Hi All"
and then:lcase a
will do the same thing as:a=$( echolcase "Hi All" )
${!1//\'/"'\''"}
instead of${!1}
allows this to work even when the string has quotes.In zsh:
Gotta love zsh!
This is a far faster variation of JaredTS486's approach that uses native Bash capabilities (including Bash versions <4.0) to optimize his approach.
I've timed 1,000 iterations of this approach for a small string (25 characters) and a larger string (445 characters), both for lowercase and uppercase conversions. Since the test strings are predominantly lowercase, conversions to lowercase are generally faster than to uppercase.
I've compared my approach with several other answers on this page that are compatible with Bash 3.2. My approach is far more performant than most approaches documented here, and is even faster than
tr
in several cases.Here are the timing results for 1,000 iterations of 25 characters:
tr
to lowercase; 3.81s for uppercaseTiming results for 1,000 iterations of 445 characters (consisting of the poem "The Robin" by Witter Bynner):
tr
to lowercase; 4s for uppercaseSolution:
The approach is simple: while the input string has any remaining uppercase letters present, find the next one, and replace all instances of that letter with its lowercase variant. Repeat until all uppercase letters are replaced.
Some performance characteristics of my solution:
UCS
andLCS
can be augmented with additional characters