Bird
Raised Fist0
Bash Scriptingscripting~20 mins

String suffix removal (${var%pattern}) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Suffix Removal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of this suffix removal?
Given the variable assignment filename="report_final_v2.txt", what is the output of echo ${filename%.txt}?
Bash Scripting
filename="report_final_v2.txt"
echo ${filename%.txt}
Areport_final_v2
Breport_final_v2.txt
Creport_final_v2.tx
Dreport_final_v2.t
Attempts:
2 left
💡 Hint
The % operator removes the shortest matching suffix pattern from the variable's value.
💻 Command Output
intermediate
1:30remaining
Removing suffix with wildcard pattern
What is the output of this command?
path="/home/user/docs/file_backup.tar.gz"
echo ${path%.tar.*}
Bash Scripting
path="/home/user/docs/file_backup.tar.gz"
echo ${path%.tar.*}
A/home/user/docs/file_backup
B/home/user/docs/file_backup.tar
C/home/user/docs/file_backup.tar.gz
D/home/user/docs/file_backup.tar.g
Attempts:
2 left
💡 Hint
The pattern %.tar.* removes the shortest suffix starting with '.tar.'
📝 Syntax
advanced
1:30remaining
Identify the correct suffix removal syntax
Which option correctly removes the shortest suffix matching '.log' from the variable file?
Bash Scripting
file="system_error.log"
Aecho ${file%\.log}
Becho ${file%.log}
Cecho ${file%log.}
Decho ${file%*.log}
Attempts:
2 left
💡 Hint
The pattern must match the suffix exactly as it appears.
🔧 Debug
advanced
2:00remaining
Why does this suffix removal fail?
Given name="archive.tar.gz.bak", why does echo ${name%.tar.gz} output archive.tar.gz.bak instead of archive?
Bash Scripting
name="archive.tar.gz.bak"
echo ${name%.tar.gz}
ABecause %.tar.gz removes the shortest suffix matching '.tar.gz', but the shell treats '.' as a wildcard and fails.
BBecause %.tar.gz removes only the shortest suffix, but '.tar.gz' is the entire suffix so it matches and removes it.
CBecause %.tar.gz removes the shortest suffix matching '.tar.gz', but the pattern must be quoted to work.
DBecause %.tar.gz removes the shortest suffix matching '.tar.gz', but the pattern is treated literally and does not match the suffix.
Attempts:
2 left
💡 Hint
The pattern must match some suffix of the variable exactly.
🚀 Application
expert
2:00remaining
Extract base filename without extension using suffix removal
You have a variable file="backup_2024-06-01.tar.gz". Which command correctly extracts the base filename backup_2024-06-01 by removing the longest suffix starting with the first dot?
Bash Scripting
file="backup_2024-06-01.tar.gz"
Aecho ${file#*.}
Becho ${file%.*}
Cecho ${file%%.*}
Decho ${file##*.}
Attempts:
2 left
💡 Hint
Use %% to remove the longest matching suffix pattern.

Practice

(1/5)
1. What does the Bash expression ${filename%.txt} do to the variable filename?
easy
A. Removes the shortest prefix matching '.txt' from the start of filename
B. Removes the longest suffix matching '.txt' from the end of filename
C. Removes the shortest suffix matching '.txt' from the end of filename
D. Removes the longest prefix matching '.txt' from the start of filename

Solution

  1. Step 1: Understand the % operator in Bash

    The single % operator removes the shortest matching suffix pattern from the variable's value.
  2. Step 2: Apply to the pattern '.txt'

    It removes '.txt' only if it appears at the end of the string, trimming that suffix.
  3. Final Answer:

    Removes the shortest suffix matching '.txt' from the end of filename -> Option C
  4. Quick 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
A. ${file%%.log}
B. ${file%.log}
C. ${file#log}
D. ${file%log}

Solution

  1. Step 1: Identify suffix removal syntax

    To remove the shortest suffix matching a pattern, use single % with the pattern including the dot: %.log
  2. Step 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.
  3. Final Answer:

    ${file%.log} -> Option B
  4. Quick 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:
filename="report.final.txt"
echo ${filename%.txt}

What is the output?
medium
A. report.final.tx
B. report
C. report.final.txt
D. report.final

Solution

  1. Step 1: Understand the variable value

    The variable filename contains 'report.final.txt'.
  2. Step 2: Apply ${filename%.txt}

    This removes the shortest suffix matching '.txt' from the end, leaving 'report.final'.
  3. Final Answer:

    report.final -> Option D
  4. Quick 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:
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
A. Use ${file%%*.gz} to remove the longest matching suffix
B. Use ${file%.gz} to remove only '.gz' suffix
C. Use ${file#*.tar.gz} to remove prefix instead
D. Use ${file%.tar} to remove only '.tar' suffix

Solution

  1. Step 1: Understand why ${file%*.gz} removes only '.gz'

    Single % removes shortest suffix matching *.gz pattern, so it matches '.gz' (with * matching empty).
  2. Step 2: Use %% to remove longest suffix

    Double %% removes longest matching suffix, so ${file%%*.gz} removes entire '.tar.gz'.
  3. Final Answer:

    Use ${file%%*.gz} to remove the longest matching suffix -> Option A
  4. Quick 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:
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
A. for f in "${files[@]}"; do echo ${f%.csv}; done
B. for f in "${files[@]}"; do echo ${f%%csv}; done
C. for f in "${files[@]}"; do echo ${f%csv}; done
D. for f in "${files[@]}"; do echo ${f#%.csv}; done

Solution

  1. Step 1: Understand the goal

    Remove '.csv' suffix only if it exists, leave other filenames unchanged.
  2. 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.
  3. Step 3: Choose best option

    for f in "${files[@]}"; do echo ${f%.csv}; done is correct and simplest for shortest suffix removal of '.csv'.
  4. Final Answer:

    for f in "${files[@]}"; do echo ${f%.csv}; done -> Option A
  5. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard