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 spite of how old this question is and similar to this answer by technosaurus. I had a hard time finding a solution that was portable across most platforms (That I Use) as well as older versions of bash. I have also been frustrated with arrays, functions and use of prints, echos and temporary files to retrieve trivial variables. This works very well for me so far I thought I would share. My main testing environments are:
Simple C-style for loop to iterate through the strings. For the line below if you have not seen anything like this before this is where I learned this. In this case the line checks if the char ${input:$i:1} (lower case) exists in input and if so replaces it with the given char ${ucs:$j:1} (upper case) and stores it back into input.
The are various ways:
POSIX standard
tr
AWK
Non-POSIX
You may run into portability issues with the following examples:
Bash 4.0
sed
Perl
Bash
Using GNU
sed
:Example:
To store the transformed string into a variable. Following worked for me -
$SOURCE_NAME
to$TARGET_NAME
Many answers using external programs, which is not really using
Bash
.If you know you will have Bash4 available you should really just use the
${VAR,,}
notation (it is easy and cool). For Bash before 4 (My Mac still uses Bash 3.2 for example). I used the corrected version of @ghostdog74 's answer to create a more portable version.One you can call
lowercase 'my STRING'
and get a lowercase version. I read comments about setting the result to a var, but that is not really portable inBash
, since we can't return strings. Printing it is the best solution. Easy to capture with something likevar="$(lowercase $str)"
.How this works
The way this works is by getting the ASCII integer representation of each char with
printf
and thenadding 32
ifupper-to->lower
, orsubtracting 32
iflower-to->upper
. Then useprintf
again to convert the number back to a char. From'A' -to-> 'a'
we have a difference of 32 chars.Using
printf
to explain:97 - 65 = 32
And this is the working version with examples.
Please note the comments in the code, as they explain a lot of stuff:
And the results after running this:
This should only work for ASCII characters though.
For me it is fine, since I know I will only pass ASCII chars to it.
I am using this for some case-insensitive CLI options, for example.
Converting case is done for alphabets only. So, this should work neatly.
I am focusing on converting alphabets between a-z from upper case to lower case. Any other characters should just be printed in stdout as it is...
Converts the all text in path/to/file/filename within a-z range to A-Z
For converting lower case to upper case
For converting from upper case to lower case
For example,
filename:
gets converted to:
Example 2:
Example 3: