String variables in Bash Scripting - Time & Space Complexity
We want to understand how the time to work with string variables changes as the string gets longer.
How does the script's running time grow when we use bigger strings?
Analyze the time complexity of the following code snippet.
string="hello"
length=${#string}
for (( i=0; i
This code prints each character of a string one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each character of the string.
- How many times: Once for each character in the string (length of the string).
As the string gets longer, the loop runs more times, once per character.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 times |
| 100 | 100 times |
| 1000 | 1000 times |
Pattern observation: The number of operations grows directly with the string length.
Time Complexity: O(n)
This means the time to process the string grows in a straight line as the string gets longer.
[X] Wrong: "Accessing each character in a string is constant time regardless of string length."
[OK] Correct: Each character access happens inside a loop that runs once per character, so total time grows with string length.
Understanding how string operations scale helps you write scripts that handle data efficiently and avoid slowdowns as input grows.
"What if we replaced the loop with a command that processes the whole string at once? How would the time complexity change?"
Practice
Hello to a variable named greeting in bash?Solution
Step 1: Understand bash variable assignment syntax
In bash, variable assignment must have no spaces around the equals sign.Step 2: Check the options for correct syntax
greeting="Hello" uses no spaces and quotes the string correctly. The other options have invalid syntax.Final Answer:
greeting="Hello" -> Option AQuick Check:
No spaces around = and quotes needed [OK]
- Adding spaces around = causes syntax errors
- Forgetting quotes around strings with spaces
- Using wrong assignment symbols like : or <-
name in bash?Solution
Step 1: Recall how to access variable values in bash
Use the dollar sign ($) before the variable name to get its value.Step 2: Evaluate each option
echo $name uses$namecorrectly. echo name prints the literal word 'name'. The other options use invalid symbols.Final Answer:
echo $name -> Option BQuick Check:
Use $ to access variable value [OK]
- Forgetting $ prints variable name, not value
- Using wrong symbols like & or %
- Confusing variable access with assignment
name="Alice" echo "Hello, $name!"
Solution
Step 1: Understand variable assignment and usage
The variablenameis assigned the string "Alice".Step 2: Analyze the echo command with variable expansion
The echo command uses double quotes, so$nameis replaced by its value "Alice".Final Answer:
Hello, Alice! -> Option AQuick Check:
Variables expand inside double quotes [OK]
- Expecting variable expansion inside single quotes
- Printing variable name literally without $
- Forgetting to assign variable before use
message = "Hi there" echo $message
Solution
Step 1: Check variable assignment syntax
In bash, no spaces are allowed around the equals sign when assigning variables.Step 2: Analyze the given script
The linemessage = "Hi there"has spaces around =, causing a syntax error.Final Answer:
Spaces around = cause syntax error -> Option DQuick Check:
No spaces around = in assignments [OK]
- Adding spaces around =
- Using wrong quotes type causes no error here
- Forgetting $ in echo prints variable name literally
fullname and then print it. Which script correctly does this?Solution
Step 1: Assign string with spaces to variable
Strings with spaces must be quoted to be assigned correctly in bash.Step 2: Print the variable value
Useecho $fullnameto print the stored string value.Final Answer:
fullname="John Doe" echo $fullname -> Option CQuick Check:
Quote strings with spaces and use $ to print [OK]
- Not quoting strings with spaces causes errors
- Printing variable name without $ prints literal text
- Using underscores instead of spaces changes the string
Ready to prove it?
Pass the test and this pattern moves unknown
