How to convert a string to lower case in Bash?

ID : 734

viewed : 199

Tags : bashstringbashshelllowercasebash





Top 5 Answer for How to convert a string to lower case in Bash?

vote vote

99

The are various ways:

POSIX standard

tr

$ echo "$a" | tr '[:upper:]' '[:lower:]' hi all 

AWK

$ echo "$a" | awk '{print tolower($0)}' hi all 

Non-POSIX

You may run into portability issues with the following examples:

Bash 4.0

$ echo "${a,,}" hi all 

sed

$ echo "$a" | sed -e 's/\(.*\)/\L\1/' hi all # this also works: $ sed -e 's/\(.*\)/\L\1/' <<< "$a" hi all 

Perl

$ echo "$a" | perl -ne 'print lc' hi all 

Bash

lc(){     case "$1" in         [A-Z])         n=$(printf "%d" "'$1")         n=$((n+32))         printf \\$(printf "%o" "$n")         ;;         *)         printf "%s" "$1"         ;;     esac } word="I Love Bash" for((i=0;i<${#word};i++)) do     ch="${word:$i:1}"     lc "$ch" done 

Note: YMMV on this one. Doesn't work for me (GNU bash version 4.2.46 and 4.0.33 (and same behaviour 2.05b.0 but nocasematch is not implemented)) even with using shopt -u nocasematch;. Unsetting that nocasematch causes [[ "fooBaR" == "FOObar" ]] to match OK BUT inside case weirdly [b-z] are incorrectly matched by [A-Z]. Bash is confused by the double-negative ("unsetting nocasematch")! :-)

vote vote

82

In Bash 4:

To lowercase

$ string="A FEW WORDS" $ echo "${string,}" a FEW WORDS $ echo "${string,,}" a few words $ echo "${string,,[AEIUO]}" a FeW WoRDS  $ string="A Few Words" $ declare -l string $ string=$string; echo "$string" a few words 

To uppercase

$ string="a few words" $ echo "${string^}" A few words $ echo "${string^^}" A FEW WORDS $ echo "${string^^[aeiou]}" A fEw wOrds  $ string="A Few Words" $ declare -u string $ string=$string; echo "$string" A FEW WORDS 

Toggle (undocumented, but optionally configurable at compile time)

$ string="A Few Words" $ echo "${string~~}" a fEW wORDS $ string="A FEW WORDS" $ echo "${string~}" a FEW WORDS $ string="a few words" $ echo "${string~}" A few words 

Capitalize (undocumented, but optionally configurable at compile time)

$ string="a few words" $ declare -c string $ string=$string $ echo "$string" A few words 

Title case:

$ string="a few words" $ string=($string) $ string="${string[@]^}" $ echo "$string" A Few Words  $ declare -c string $ string=(a few words) $ echo "${string[@]}" A Few Words  $ string="a FeW WOrdS" $ string=${string,,} $ string=${string~} $ echo "$string" A few words 

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.

vote vote

73

echo "Hi All" | tr "[:upper:]" "[:lower:]" 
vote vote

64

tr:

a="$(tr [A-Z] [a-z] <<< "$a")" 

AWK:

{ print tolower($0) } 

sed:

y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/ 
vote vote

60

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:

b=`echo "print '$a'.lower()" | python` 

Or Ruby:

b=`echo "print '$a'.downcase" | ruby` 

Or Perl:

b=`perl -e "print lc('$a');"` 

Or PHP:

b=`php -r "print strtolower('$a');"` 

Or Awk:

b=`echo "$a" | awk '{ print tolower($1) }'` 

Or Sed:

b=`echo "$a" | sed 's/./\L&/g'` 

Or Bash 4:

b=${a,,} 

Or NodeJS:

b=`node -p "\"$a\".toLowerCase()"` 

You could also use dd:

b=`echo "$a" | dd  conv=lcase 2> /dev/null` 

lower -> UPPER:

use python:

b=`echo "print '$a'.upper()" | python` 

Or Ruby:

b=`echo "print '$a'.upcase" | ruby` 

Or Perl:

b=`perl -e "print uc('$a');"` 

Or PHP:

b=`php -r "print strtoupper('$a');"` 

Or Awk:

b=`echo "$a" | awk '{ print toupper($1) }'` 

Or Sed:

b=`echo "$a" | sed 's/./\U&/g'` 

Or Bash 4:

b=${a^^} 

Or NodeJS:

b=`node -p "\"$a\".toUpperCase()"` 

You could also use dd:

b=`echo "$a" | dd  conv=ucase 2> /dev/null` 

Also when you say 'shell' I'm assuming you mean bash but if you can use zsh it's as easy as

b=$a:l 

for lower case and

b=$a:u 

for upper case.

Top 3 video Explaining How to convert a string to lower case in Bash?







Related QUESTION?