Is there a way in Bash shell scripting so that I can convert a string into lower case string?
For example,
if $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:
|
|||||||||||||||||||||
|
In zsh:
Gotta love zsh! |
|||||||||||||
|
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. |
|||||
|
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. |
|||||||||
|
In bash 4 you can use typeset Example:
|
||||
|
Using GNU
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:
|
|||
|
Using Korn shell (ksh) typeset:
|
||||
|
Thank you for your interest in this question.
Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?