0
0
Bash Scriptingscripting~10 mins

String replacement (${var/old/new}) in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - String replacement (${var/old/new})
Start with variable var
Check for 'old' substring in var
Yes
Replace first occurrence of 'old' with 'new'
Output modified string
End
This flow shows how bash replaces the first occurrence of a substring 'old' with 'new' inside a variable's value.
Execution Sample
Bash Scripting
var="hello world"
new_var=${var/world/universe}
echo "$new_var"
Replace 'world' with 'universe' in the string stored in var and print the result.
Execution Table
StepVariable varOperationResultOutput
1"hello world"Initial value assigned"hello world"
2"hello world"Replace 'world' with 'universe'"hello universe"
3"hello world"Assign to new_var"hello universe"
4"hello world"Print new_var"hello universe"hello universe
5End of script
💡 Script ends after printing the replaced string.
Variable Tracker
VariableStartAfter ReplacementFinal
var"hello world""hello world""hello world"
new_var"hello universe""hello universe"
Key Moments - 2 Insights
Why does only the first occurrence of 'old' get replaced?
Because the syntax ${var/old/new} replaces only the first match. To replace all, you would use ${var//old/new}. See execution_table step 2.
What happens if 'old' is not found in the string?
No replacement occurs, and the original string remains unchanged. This is shown by the initial assignment in step 1 and no change in step 2 if 'old' is missing.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of new_var after step 2?
A"hello world"
B"universe world"
C"hello universe"
D"hello"
💡 Hint
Check the 'Result' column in row for step 2 in execution_table.
At which step is the replaced string printed?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look for the 'Print new_var' operation in execution_table.
If the variable var was "world world", what would new_var be after replacement?
A"universe world"
B"universe universe"
C"world universe"
D"world world"
💡 Hint
Remember ${var/old/new} replaces only the first occurrence, see key_moments explanation.
Concept Snapshot
String replacement in bash uses ${var/old/new} to replace the first occurrence of 'old' with 'new' in var.
To replace all occurrences, use ${var//old/new}.
If 'old' is not found, the string stays the same.
This is useful for quick text changes inside variables.
Full Transcript
This lesson shows how to replace part of a string stored in a variable in bash scripting. We start with a variable var containing a string. Using the syntax ${var/old/new}, bash replaces the first occurrence of the substring 'old' with 'new'. The replaced string is then stored in a new variable or used directly. We traced the steps: assigning the initial string, performing the replacement, assigning the result, and printing it. We also clarified that only the first match is replaced unless you use double slashes. If the substring is not found, the string remains unchanged. This helps beginners understand how to manipulate strings easily in bash scripts.