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:
$a="hi all"
|
The are various ways: tr
AWK
Bash 4.0
Perl
Bash
|
|||||||||||||||||||||
|
In Bash 4: To lowercase
To uppercase
Toggle (undocumented)
Capitalize (undocumented)
Title case:
To turn off a Edit: Added "toggle first character by word" ( |
|||||||||||||||||||||
|
|
|||||||||||||
|
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
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
Also when you say 'shell' I'm assuming you mean
for lower case and
for upper case. |
|||||||||||||||||
|
In zsh:
Gotta love zsh! |
|||||||||||||
|
For a standard shell (without bashisms) using only builtins:
And for upper case:
|
|||||||||
|
Regular expressionI would like to take credit for the command I wish to share but the truth is I obtained it for my own use from http://commandlinefu.com. It has the advantage that if you
You can specify a directory in place of the dot(.) after the find which denotes current directory or full path. I hope this solution proves useful the one thing this command does not do is replace spaces with underscores - oh well another time perhaps. |
|||||||||||||
|
Using GNU
Example:
|
||||
|
In bash 4 you can use typeset Example:
|
||||
|
For Bash versions earlier than 4.0, this version should be fastest (as it doesn't fork/exec any commands):
technosaurus's answer had potential too, although it did run properly for mee. |
||||
|
Pre Bash 4.0 Bash Lower the Case of a string and assign to variable
|
|||||||||||||
|
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:
|
|||
|
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.
|
|||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?