0
0
Bash Scriptingscripting~15 mins

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

Choose your learning style9 modes available
Remove Prefix from Filenames Using Bash String Manipulation
📖 Scenario: You have a list of filenames that all start with the prefix temp_. You want to clean up these names by removing this prefix.
🎯 Goal: Build a bash script that removes the prefix temp_ from each filename using the ${var#pattern} string prefix removal syntax.
📋 What You'll Learn
Create a bash array called files with the exact filenames: temp_report.txt, temp_data.csv, temp_notes.md
Create a variable called prefix and set it to temp_
Use a for loop with variable file to iterate over files
Inside the loop, create a variable called clean_name that removes the prefix temp_ from file using ${file#${prefix}}
Print each clean_name on its own line
💡 Why This Matters
🌍 Real World
Removing prefixes from filenames is common when organizing files, cleaning up logs, or processing data exports.
💼 Career
Knowing bash string manipulation helps automate file management tasks efficiently in system administration and DevOps roles.
Progress0 / 4 steps
1
Create the list of filenames
Create a bash array called files with these exact filenames: temp_report.txt, temp_data.csv, temp_notes.md
Bash Scripting
Need a hint?

Use parentheses () to create an array and double quotes "" around each filename.

2
Set the prefix variable
Create a variable called prefix and set it to the exact string temp_
Bash Scripting
Need a hint?

Use prefix="temp_" to assign the string.

3
Remove the prefix using a loop
Use a for loop with variable file to iterate over files. Inside the loop, create a variable called clean_name that removes the prefix temp_ from file using ${file#${prefix}}
Bash Scripting
Need a hint?

Use for file in "${files[@]}" to loop over the array. Use clean_name=${file#${prefix}} to remove the prefix.

4
Print the cleaned filenames
Inside the existing for loop, add a echo statement to print the variable clean_name on its own line
Bash Scripting
Need a hint?

Use echo "$clean_name" inside the loop to print each cleaned filename.