Bash - Capitalize every words

By xngo on June 16, 2019

#!/bin/bash
set -e
# Description: Capitalize every input words.
 
function capitalizeFirstChar()
{
    local string=$1
 
    # Capitalize first character.
    first_char=${string:0:1}
        first_char=$(echo "${first_char}" | tr '[:lower:]' '[:upper:]')
 
    # Get the full string except the 1st character.
    right_side=${string:1}
 
    echo -n "${first_char}${right_side} "
}
 
# Capitalize every input words.
for arg in "${@}"; do
    capitalizeFirstChar  "${arg}"
done

Output

$./capitalize.sh this is a test
This Is A Test 

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.