Skip to main content
trifles
Source Link
greybeard
  • 2.4k
  • 9
  • 36
  • 70

There are various ways:

POSIX standard

trPOSIX standard

tr

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

AWK

AWK

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

Non-POSIX

You may run into portability issues with the following examples:

Bash 4.0

Bash 4.0

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

sed

sed

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

Perl

Perl

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

Bash

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 isnocasematch is not implemented)) even with using shopt -u nocasematch;. Unsetting that nocasematchnocasematch causes [[ "fooBaR" == "FOObar" ]][[ "fooBaR" == "FOObar" ]] to match OK BUT inside case weirdly [b-z][b-z] are incorrectly matched by [A-Z][A-Z]. Bash is confused by the double-negative ("unsetting nocasematch")! :-)

There 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")! :-)

There 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")! :-)

TheThere are various ways:

POSIX standard

###tr

tr

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

###AWK

AWK

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

Non-POSIX

##Non-POSIX## YouYou may run into portability issues with the following examples:

###Bash 4.0

Bash 4.0

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

###sed

sed

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

###Perl

Perl

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

###Bash

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")! :-)

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")! :-)

There 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")! :-)

Note on shopt -u nocasematch and what seems to be bug in bash case behaviour.
Source Link
gaoithe
  • 4.3k
  • 3
  • 32
  • 39

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")! :-)

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

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")! :-)

Adding some critical information from a comment. POSIX info should persist with the answer.
Source Link
Loading
Add sed to list at position 4
Source Link
Ross Smith II
  • 12.1k
  • 1
  • 39
  • 45
Loading
The Bash example was only processing uppercase chars. Lowercase chars and other symbols were being ignored and discarded. Fixed and changed the example $word to one that have lowercase letters and spaces as well.
Source Link
Martin Tournoij
  • 27.5k
  • 24
  • 107
  • 153
Loading
The Bash example was only processing uppercase chars. Lowercase chars and other symbols were being ignored and discarded. Fixed and changed the example $word to one that have lowercase letters and spaces as well.
Source Link
Loading
Add proper quoting everywhere
Source Link
tripleee
  • 185.2k
  • 36
  • 295
  • 342
Loading
deleted 1 characters in body
Source Link
Zombo
  • 1
  • 66
  • 403
  • 423
Loading
added links for Bash
Source Link
Jared Burrows
  • 55.1k
  • 26
  • 154
  • 189
Loading
Use tr '[:upper:]' '[:lower:]' as suggested in comments
Source Link
John Kugelman
  • 357.7k
  • 69
  • 542
  • 584
Loading
Copy edited. Applied some formatting (sub section titles). Added some context.
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 109
  • 132
Loading
added 293 characters in body
Source Link
ghostdog74
  • 337.4k
  • 58
  • 260
  • 348
Loading
Source Link
ghostdog74
  • 337.4k
  • 58
  • 260
  • 348
Loading