uppercase first character in a variable with bash

前端 未结 15 1601
一整个雨季
一整个雨季 2020-12-04 07:01

I want to uppercase just the first character in my string with bash.

foo=\"bar\";

//uppercase first character

echo $foo;

should print \"B

相关标签:
15条回答
  • 2020-12-04 07:47

    Using awk only

    foo="uNcapItalizedstrIng"
    echo $foo | awk '{print toupper(substr($0,0,1))tolower(substr($0,2))}'
    
    0 讨论(0)
  • 2020-12-04 07:49
    first-letter-to-lower () {
            str="" 
            space=" " 
            for i in $@
            do
                    if [ -z $(echo $i | grep "the\|of\|with" ) ]
                    then
                            str=$str"$(echo ${i:0:1} | tr  '[A-Z]' '[a-z]')${i:1}$space" 
                    else
                            str=$str${i}$space 
                    fi
            done
            echo $str
    }
    first-letter-to-upper-xc () {
            v-first-letter-to-upper | xclip -selection clipboard
    }
    first-letter-to-upper () {
            str="" 
            space=" " 
            for i in $@
            do
                    if [ -z $(echo $i | grep "the\|of\|with" ) ]
                    then
                            str=$str"$(echo ${i:0:1} | tr  '[a-z]' '[A-Z]')${i:1}$space" 
                    else
                            str=$str${i}$space 
                    fi
            done
            echo $str
    }
    

    first-letter-to-lower-xc(){ v-first-letter-to-lower | xclip -selection clipboard }

    0 讨论(0)
  • 2020-12-04 07:50

    Alternative and clean solution for both Linux and OSX, it can also be used with bash variables

    python -c "print(\"abc\".capitalize())"
    

    returns Abc

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