This helps you cut off the ending part of a word or sentence stored in a variable. It is useful when you want to remove a known ending.
String suffix removal (${var%pattern}) in Bash Scripting
Introduction
Syntax
Bash Scripting
${variable%pattern}The % removes the shortest match of pattern from the end of the variable's value.
Use %% to remove the longest match from the end.
Examples
.txt from the filename, leaving just report.Bash Scripting
filename="report.txt"
name=${filename%.txt}Bash Scripting
path="/home/user/docs/"
clean_path=${path%/}.html suffix from the URL string.Bash Scripting
url="example.com/page.html"
base=${url%.html}Sample Program
This script removes the .jpeg suffix from the filename and prints both original and base name.
Bash Scripting
#!/bin/bash file="photo.jpeg" # Remove the shortest suffix matching .jpeg base_name=${file%.jpeg} echo "Original file: $file" echo "Base name: $base_name"
Important Notes
If the pattern does not match the end of the string, the variable remains unchanged.
Patterns use shell globbing, so * and ? can be used.
Summary
${var%pattern} removes the shortest matching suffix from a variable.
It is useful for trimming file extensions or trailing characters.
Use %% for the longest match removal.
Practice
1. What does the Bash expression
${filename%.txt} do to the variable filename?easy
Solution
Step 1: Understand the % operator in Bash
The single % operator removes the shortest matching suffix pattern from the variable's value.Step 2: Apply to the pattern '.txt'
It removes '.txt' only if it appears at the end of the string, trimming that suffix.Final Answer:
Removes the shortest suffix matching '.txt' from the end of filename -> Option CQuick Check:
${var%pattern} removes shortest suffix [OK]
Hint: Single % removes shortest suffix, double %% removes longest [OK]
Common Mistakes:
- Confusing % with prefix removal operators
- Using %% instead of %
- Thinking it removes prefix instead of suffix
2. Which of the following is the correct syntax to remove the shortest suffix '.log' from the variable
file in Bash?easy
Solution
Step 1: Identify suffix removal syntax
To remove the shortest suffix matching a pattern, use single % with the pattern including the dot: %.logStep 2: Check each option
A uses %% which removes longest suffix, C uses # which removes prefix, D misses the dot before 'log'. Only B is correct syntax.Final Answer:
${file%.log} -> Option BQuick Check:
Use single % with pattern for shortest suffix removal [OK]
Hint: Use single % with exact suffix pattern to remove shortest suffix [OK]
Common Mistakes:
- Using %% instead of %
- Confusing % with # for prefix removal
- Omitting the dot in the pattern
3. Given the Bash code:
What is the output?
filename="report.final.txt"
echo ${filename%.txt}What is the output?
medium
Solution
Step 1: Understand the variable value
The variable filename contains 'report.final.txt'.Step 2: Apply ${filename%.txt}
This removes the shortest suffix matching '.txt' from the end, leaving 'report.final'.Final Answer:
report.final -> Option DQuick Check:
Suffix '.txt' removed once from end [OK]
Hint: Remove suffix by matching pattern at string end with % [OK]
Common Mistakes:
- Removing more than '.txt' suffix
- Confusing prefix removal with suffix removal
- Expecting the full string unchanged
4. Consider this Bash snippet:
It outputs
file="archive.tar.gz"
echo ${file%*.gz}It outputs
archive.tar. You want to remove the entire suffix .tar.gz. What is the fix?medium
Solution
Step 1: Understand why ${file%*.gz} removes only '.gz'
Single % removes shortest suffix matching *.gz pattern, so it matches '.gz' (with * matching empty).Step 2: Use %% to remove longest suffix
Double %% removes longest matching suffix, so ${file%%*.gz} removes entire '.tar.gz'.Final Answer:
Use ${file%%*.gz} to remove the longest matching suffix -> Option AQuick Check:
%% removes longest suffix, % removes shortest [OK]
Hint: Use %% to remove longest suffix pattern [OK]
Common Mistakes:
- Using single % when longest suffix removal needed
- Confusing prefix removal (#) with suffix removal (%)
- Trying to remove partial suffix only
5. You have a list of filenames:
You want to remove the '.csv' suffix only from files that end with '.csv' using Bash parameter expansion. Which command correctly prints the filenames with '.csv' removed where applicable?
files=("data1.csv" "data2.csv" "notes.txt" "summary.csv")You want to remove the '.csv' suffix only from files that end with '.csv' using Bash parameter expansion. Which command correctly prints the filenames with '.csv' removed where applicable?
hard
Solution
Step 1: Understand the goal
Remove '.csv' suffix only if it exists, leave other filenames unchanged.Step 2: Check each option's effect
A uses single % with '.csv' pattern, removing suffix if present. B uses %%csv missing the dot, incorrect. C misses the dot, incorrect. D uses # which removes prefix, wrong direction.Step 3: Choose best option
for f in "${files[@]}"; do echo ${f%.csv}; done is correct and simplest for shortest suffix removal of '.csv'.Final Answer:
for f in "${files[@]}"; do echo ${f%.csv}; done -> Option AQuick Check:
Use ${var%.csv} to remove '.csv' suffix if present [OK]
Hint: Use ${var%.ext} in loop to trim suffix conditionally [OK]
Common Mistakes:
- Using prefix removal (#) instead of suffix removal (%)
- Omitting the dot in suffix pattern
- Using %% unnecessarily when % suffices
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
