0
0
Bash Scriptingscripting~10 mins

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

Choose your learning style9 modes available
String suffix removal (${var%pattern})
📖 Scenario: You have a list of filenames with extensions. You want to get just the base names without the extensions.
🎯 Goal: Learn how to use the ${var%pattern} syntax in bash to remove suffixes from strings.
📋 What You'll Learn
Create a variable with a filename including an extension
Create a variable with the extension pattern to remove
Use ${var%pattern} to remove the extension from the filename
Print the filename without the extension
💡 Why This Matters
🌍 Real World
Removing file extensions is common when you want to process or rename files without their suffixes.
💼 Career
Shell scripting skills like string manipulation are useful for automation tasks in system administration and DevOps.
Progress0 / 4 steps
1
Create a variable with a filename
Create a variable called filename and set it to the string report.txt.
Bash Scripting
Need a hint?

Use filename="report.txt" to assign the string.

2
Create a variable with the extension pattern
Create a variable called ext and set it to the string .txt.
Bash Scripting
Need a hint?

Use ext=".txt" to assign the extension pattern.

3
Remove the extension using ${var%pattern}
Create a variable called base that removes the suffix stored in ext from filename using ${filename%$ext}.
Bash Scripting
Need a hint?

Use base=${filename%$ext} to remove the suffix.

4
Print the filename without the extension
Print the value of base using echo.
Bash Scripting
Need a hint?

Use echo "$base" to display the result.