uppercase first character in a variable with bash

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

    To capitalize first word only:

    foo='one two three'
    foo="${foo^}"
    echo $foo
    

    One two three


    To capitalize every word in the variable:

    foo="one two three"
    foo=( $foo ) # without quotes
    foo="${foo[@]^}"
    echo $foo
    

    One Two Three


    (works in bash 4+)

提交回复
热议问题