You are on page 1of 1

linux capitalize the first letter in a variable

ChatGPT
You can capitalize the first letter of a string stored in a variable in Linux bash
shell using the following method:

bash
Copy code
# Example input string in a variable
input="hello world"

# Capitalize the first letter


output="$(tr '[:lower:]' '[:upper:]' <<< "${input:0:1}")${input:1}"

# Print the capitalized string


echo "$output"
Here's what each part of the script does:

input="hello world": This line sets the input string to your variable. Replace
"hello world" with your actual string.

output="$(tr '[:lower:]' '[:upper:]' <<< "${input:0:1}")${input:1}": This line


performs the capitalization. It takes the first character of the input string,
converts it to uppercase using tr, and then concatenates it with the rest of the
string (from position 1 onward).

echo "$output": This line prints the capitalized string.

When you run this script, it will capitalize the first letter of the string stored
in the input variable and display the result.

You might also like