uppercase first character in a variable with bash

前端 未结 15 1761
一整个雨季
一整个雨季 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:29

    This works too...

    FooBar=baz
    
    echo ${FooBar^^${FooBar:0:1}}
    
    => Baz
    
    FooBar=baz
    
    echo ${FooBar^^${FooBar:1:1}}
    
    => bAz
    
    FooBar=baz
    
    echo ${FooBar^^${FooBar:2:2}}
    
    => baZ
    

    And so on.

    Sources:

    • Bash Manual: Shell Parameter Expansion
    • Full Bash Guide: Parameters
    • Bash Hacker's Wiki Parameter Expansion

    Inroductions/Tutorials:

    • Cyberciti.biz: 8. Convert to upper to lower case or vice versa
    • Opensource.com: An introduction to parameter expansion in Bash

提交回复
热议问题