If you append ^
to a variable, Bash capitalises the first letter of its contents. (Similarly, ,
sends it to lowercase and doubling-up either of these applies the transformation to the whole string, rather than just the first letter.)
foo="hello world"
echo ${foo^} # Hello world
You can also do ${variable:position:length}
to extract a substring:
echo ${foo:0:1} # h
So far, I haven't found a way to combine these without, obviously, creating a temporary variable. Is there a form where I can get just the capitalised first letter out of an arbitrary string?
No. Parameter expansion operators do not compose, so if you want more than one side effect, you need a temporary variable (which can include overwriting the original value as shown by @fred) or an external tool to process the result of the expansion (as shown by @anubhava).
Your other alternative is to use a different shell that does support more complicated operations, like zsh
:
% foo="hello world"
% % print ${(U)${foo:0:1}}
H