How to convert a string to lower case in Bash?

前端 未结 20 2020
慢半拍i
慢半拍i 2020-11-22 09:40

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:<

相关标签:
20条回答
  • 2020-11-22 10:00

    In zsh:

    echo $a:u
    

    Gotta love zsh!

    0 讨论(0)
  • 2020-11-22 10:00

    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

    cat path/to/file/filename | tr 'a-z' 'A-Z'
    

    For converting from upper case to lower case

    cat path/to/file/filename | tr 'A-Z' 'a-z'
    

    For example,

    filename:

    my name is xyz
    

    gets converted to:

    MY NAME IS XYZ
    

    Example 2:

    echo "my name is 123 karthik" | tr 'a-z' 'A-Z'
    # Output:
    # MY NAME IS 123 KARTHIK
    

    Example 3:

    echo "my name is 123 &&^&& #@$#@%%& kAR2~thik" | tr 'a-z' 'A-Z'
    # Output:
    # MY NAME IS 123 &&^&& #@0@%%& KAR2~THIK
    
    0 讨论(0)
  • 2020-11-22 10:00

    To store the transformed string into a variable. Following worked for me - $SOURCE_NAME to $TARGET_NAME

    TARGET_NAME="`echo $SOURCE_NAME | tr '[:upper:]' '[:lower:]'`"
    
    0 讨论(0)
  • 2020-11-22 10:01

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

    0 讨论(0)
  • 2020-11-22 10:02

    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:

    1. GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
    2. GNU bash, version 3.2.57(1)-release (sparc-sun-solaris2.10)
    lcs="abcdefghijklmnopqrstuvwxyz"
    ucs="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    input="Change Me To All Capitals"
    for (( i=0; i<"${#input}"; i++ )) ; do :
        for (( j=0; j<"${#lcs}"; j++ )) ; do :
            if [[ "${input:$i:1}" == "${lcs:$j:1}" ]] ; then
                input="${input/${input:$i:1}/${ucs:$j:1}}" 
            fi
        done
    done
    

    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.

    input="${input/${input:$i:1}/${ucs:$j:1}}"
    
    0 讨论(0)
  • 2020-11-22 10:06

    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 (probably my favorite):

    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 if you have it (and are a bit nuts...):

    b=`echo "console.log('$a'.toLowerCase());" | node`
    

    You could also use dd (but I wouldn't!):

    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 (probably my favorite):

    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 if you have it (and are a bit nuts...):

    b=`echo "console.log('$a'.toUpperCase());" | node`
    

    You could also use dd (but I wouldn't!):

    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.

    0 讨论(0)
提交回复
热议问题