0
0
Bash Scriptingscripting~10 mins

String replacement (${var/old/new}) in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
String Replacement Using ${var/old/new} in Bash
📖 Scenario: You are working on a bash script that processes filenames. Sometimes, you need to rename files by replacing parts of their names.
🎯 Goal: Learn how to use bash string replacement syntax ${var/old/new} to change parts of a string.
📋 What You'll Learn
Create a variable with a filename string
Create a variable with the old text to replace
Use bash string replacement syntax to replace the old text with new text
Print the updated filename
💡 Why This Matters
🌍 Real World
Renaming files or modifying strings in scripts is common when organizing data or automating tasks.
💼 Career
Knowing string replacement in bash helps automate file management and text processing in many IT and DevOps roles.
Progress0 / 4 steps
1
Create the filename variable
Create a variable called filename and set it to the string report_draft.txt.
Bash Scripting
Need a hint?

Use filename="report_draft.txt" to create the variable.

2
Create the old text variable
Create a variable called old_text and set it to the string draft.
Bash Scripting
Need a hint?

Use old_text="draft" to create the variable.

3
Replace old text with new text
Create a variable called new_filename that replaces draft in filename with final using the bash string replacement syntax ${filename/draft/final}.
Bash Scripting
Need a hint?

Use new_filename=${filename/draft/final} to replace the text.

4
Print the new filename
Print the value of new_filename using echo.
Bash Scripting
Need a hint?

Use echo "$new_filename" to print the result.