Last Updated: February 25, 2016
·
8.386K
· victorbrca

Converting the value of a variable to upper case with Bash 4

Let's take the built-in variable "USER".

$ echo $USER
victor

Converting first letter to capital:

$ echo ${USER^}
Victor

Converting all letters to capital:

$ echo ${USER^^}
VICTOR

Converting the first letter if matches letter (v for this example)

$ echo ${USER^v}
Victor

Converting any letter that matches (c for this example)

$ echo ${USER^^c}
viCtor

And of course we can apply a loop to convert the first character of all words in a string to capital:

$ var="this is a text title"

$ echo $var
this is a text title

$ for i in $(echo $var) ; do echo ${i^} ; done | tr '\n' ' ' ; echo -e "\r"
This Is A Text Title