String length (${#var}) in Bash Scripting - Time & Space Complexity
We want to understand how the time it takes to get the length of a string changes as the string gets longer.
Specifically, how does using ${#var} behave when the string size grows?
Analyze the time complexity of the following code snippet.
mystring="Hello, world!"
length=${#mystring}
echo "Length is $length"
This code gets the length of the string stored in mystring and prints it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Counting each character in the string to find its length.
- How many times: Once per character in the string.
As the string gets longer, the time to count its length grows in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 character checks |
| 100 | 100 character checks |
| 1000 | 1000 character checks |
Pattern observation: The work grows linearly as the string length increases.
Time Complexity: O(n)
This means the time to get the string length grows directly with the number of characters in the string.
[X] Wrong: "Getting the string length is instant no matter how long the string is."
[OK] Correct: The system must check each character to count length, so longer strings take more time.
Knowing how simple operations like string length scale helps you reason about script performance and write efficient code.
"What if the string length was stored in a variable and updated as the string changes? How would that affect the time complexity of getting the length?"
Practice
${#var} do in a bash script?Solution
Step 1: Understand the syntax
This syntax in bash returns the length of the string stored in the variable${#var}var.Step 2: Differentiate from other operations
It does not return the value itself, count words, or change case. It specifically counts characters.Final Answer:
It returns the length of the string stored in the variable var. -> Option DQuick Check:
Length of string = ${#var} [OK]
- Confusing ${#var} with $var (value of variable)
- Thinking it counts words instead of characters
- Assuming it changes the string content
name in bash?Solution
Step 1: Recall correct syntax for string length
The correct syntax to get string length is${#variable}, so fornameit is${#name}.Step 2: Check each option
length=${name#} uses wrong placement of #, length=$#name uses $ and # incorrectly, length=$name# appends # after variable incorrectly.Final Answer:
length=${#name} -> Option CQuick Check:
Use ${#var} to get length [OK]
- Placing # after variable name instead of before
- Using $# which counts positional parameters, not string length
- Confusing variable expansion syntax
text="Hello World"
echo ${#text}Solution
Step 1: Count characters in the string "Hello World"
The string has 11 characters: 5 letters in "Hello", 1 space, and 5 letters in "World".Step 2: Understand ${#text} returns string length
The commandecho ${#text}prints the length, which is 11.Final Answer:
11 -> Option AQuick Check:
Length of "Hello World" = 11 [OK]
- Ignoring spaces in length count
- Counting words instead of characters
- Expecting output to be string value instead of length
myvar=Hello length=$#myvar echo $length
What is the problem with this script?
Solution
Step 1: Understand what $# means in bash
The symbol$#returns the number of positional parameters passed to the script, not the length of a string variable.Step 2: Analyze the script behavior
Assigninglength=$#myvartries to get positional parameters count, ignoringmyvar. So it does not get string length.Final Answer:
It will print the number of positional parameters, not string length. -> Option AQuick Check:
$# counts parameters, not string length [OK]
- Confusing $# (positional params) with ${#var} (string length)
- Expecting $# to work on variables
- Missing braces around variable name
input is empty or not in a bash script. Which condition correctly uses string length to do this?Solution
Step 1: Use ${#input} to get string length
To check if input is empty, we check if its length is zero using${#input}.Step 2: Write correct if condition syntax
The condition[ ${#input} -eq 0 ]correctly tests if the string length is zero.Final Answer:
if [ ${#input} -eq 0 ]; then echo "Empty input"; fi -> Option BQuick Check:
Check empty string with ${#var} == 0 [OK]
- Using $#input which is invalid
- Comparing string value directly to number
- Misplacing # symbol in variable expansion
Ready to prove it?
Pass the test and this pattern moves unknown
