0
0
Bash Scriptingscripting~10 mins

String length (${#var}) in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Counting Characters in a Bash String
📖 Scenario: You are working on a simple bash script to help count the number of characters in a word. This is useful when you want to check the length of names, passwords, or any text input in your scripts.
🎯 Goal: Build a bash script that stores a word in a variable, then calculates and shows the number of characters in that word using the ${#var} syntax.
📋 What You'll Learn
Create a variable called word with the exact value hello
Create a variable called length that stores the length of word using ${#word}
Print the value of length to show the number of characters in word
💡 Why This Matters
🌍 Real World
Counting string length is useful in scripts that validate user input, like checking password length or formatting text.
💼 Career
Many automation and scripting jobs require handling text data and validating input lengths to ensure correct processing.
Progress0 / 4 steps
1
Create the word variable
Create a variable called word and set it to the exact string hello.
Bash Scripting
Need a hint?

Use word="hello" to assign the string.

2
Calculate the length of the word
Create a variable called length that stores the length of the string in word using ${#word}.
Bash Scripting
Need a hint?

Use length=${#word} to get the string length.

3
Print the length
Use echo to print the value of the variable length.
Bash Scripting
Need a hint?

Use echo "$length" to display the length.

4
Run the script to see the output
Run the script and confirm it prints the number 5, which is the length of the word hello.
Bash Scripting
Need a hint?

The output should be 5 because the word "hello" has 5 letters.