Bird
Raised Fist0
Bash Scriptingscripting~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Prefix Removal Pro
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 prefix removal?
Given the variable file="backup_2024_report.txt", what is the output of echo ${file#backup_}?
Bash Scripting
file="backup_2024_report.txt"
echo ${file#backup_}
Areport.txt
Bbackup_2024_report.txt
C_2024_report.txt
D2024_report.txt
Attempts:
2 left
💡 Hint
The # operator removes the shortest match of the pattern from the start of the string.
💻 Command Output
intermediate
1:30remaining
Removing shortest vs longest prefix
Given path="/home/user/docs/file.txt", what is the output of echo ${path##*/}?
Bash Scripting
path="/home/user/docs/file.txt"
echo ${path##*/}
Afile.txt
B/file.txt
Cdocs/file.txt
D/home/user/docs/file.txt
Attempts:
2 left
💡 Hint
The ## operator removes the longest match of the pattern from the start.
🔧 Debug
advanced
2:00remaining
Why does this prefix removal fail?
Consider var="version1.2.3". What error or output results from echo ${var#version.}?
Bash Scripting
var="version1.2.3"
echo ${var#version.}
A1.2.3
Bversion1.2.3
CSyntax error
Dersion1.2.3
Attempts:
2 left
💡 Hint
The pattern must match exactly at the start to be removed.
🧠 Conceptual
advanced
1:30remaining
What does this prefix removal pattern do?
Given text="error_404_not_found", what is the output of echo ${text#*_}?
Bash Scripting
text="error_404_not_found"
echo ${text#*_}
A404_not_found
Berror_404_not_found
Cnot_found
D_404_not_found
Attempts:
2 left
💡 Hint
The pattern '*_' removes the shortest prefix ending with underscore.
🚀 Application
expert
2:30remaining
Extract filename without extension using prefix removal
Given filename="archive.tar.gz", which command outputs tar.gz by removing the shortest prefix ending with a dot?
Bash Scripting
filename="archive.tar.gz"
echo ???
Aecho ${filename#*.tar}
Becho ${filename##*.}
Cecho ${filename#*.}
Decho ${filename#archive.}
Attempts:
2 left
💡 Hint
Use # to remove shortest prefix matching pattern including a dot.

Practice

(1/5)
1. What does the Bash expression ${var#pattern} do?
easy
A. Removes the shortest matching prefix pattern from the variable's value
B. Removes the longest matching suffix pattern from the variable's value
C. Removes the shortest matching suffix pattern from the variable's value
D. Removes the longest matching prefix pattern from the variable's value

Solution

  1. Step 1: Understand the syntax

    The syntax ${var#pattern} is used in Bash to remove a prefix pattern from the start of the variable's value.
  2. Step 2: Identify shortest prefix removal

    The single # removes the shortest matching prefix, unlike ## which removes the longest.
  3. Final Answer:

    Removes the shortest matching prefix pattern from the variable's value -> Option A
  4. Quick Check:

    Prefix removal = shortest match = A [OK]
Hint: Single # removes shortest prefix, double ## removes longest [OK]
Common Mistakes:
  • Confusing # with ## for prefix length
  • Mixing prefix removal with suffix removal
  • Thinking it removes suffix instead of prefix
2. Which of the following is the correct syntax to remove the shortest prefix matching abc from variable var in Bash?
easy
A. ${var#abc}
B. ${var##abc}
C. ${var%abc}
D. ${var%%abc}

Solution

  1. Step 1: Recall prefix removal syntax

    Single # removes shortest prefix, double ## removes longest prefix. % and %% remove suffixes.
  2. Step 2: Match the pattern

    To remove shortest prefix matching 'abc', use ${var#abc}.
  3. Final Answer:

    ${var#abc} -> Option A
  4. Quick Check:

    Shortest prefix removal = single # = B [OK]
Hint: Use single # for shortest prefix removal syntax [OK]
Common Mistakes:
  • Using ## instead of # for shortest prefix
  • Using % or %% which remove suffixes
  • Confusing prefix and suffix removal syntax
3. Given var="folder/subfolder/file.txt", what is the output of echo ${var#*/}?
medium
A. folder/subfolder/file.txt
B. subfolder/file.txt
C. file.txt
D. folder

Solution

  1. Step 1: Understand the pattern */

    The pattern */ matches the shortest prefix ending with a slash.
  2. Step 2: Remove shortest prefix matching */

    From "folder/subfolder/file.txt", the shortest prefix matching */ is "folder/". Removing it leaves "subfolder/file.txt".
  3. Final Answer:

    subfolder/file.txt -> Option B
  4. Quick Check:

    Remove shortest prefix ending with / = subfolder/file.txt [OK]
Hint: Pattern */ removes up to first slash [OK]
Common Mistakes:
  • Removing longest prefix with ## instead of #
  • Expecting only filename instead of subfolder/file.txt
  • Confusing prefix removal with suffix removal
4. Consider the script snippet:
var="abc123abc456"
echo ${var##abc*}
It outputs an empty line. Why?
medium
A. Because the pattern is invalid and causes an error
B. Because abc* matches the entire string as shortest prefix
C. Because abc* matches the longest prefix, so nothing remains
D. Because the variable is empty

Solution

  1. Step 1: Analyze the pattern abc*

    The pattern abc* matches 'abc' followed by zero or more any characters.
  2. Step 2: Understand longest prefix removal

    With ##, it removes the longest matching prefix. The * matches greedily as many characters as possible after 'abc', so the match covers the entire string.
  3. Final Answer:

    Because abc* matches the longest prefix, so nothing remains -> Option C
  4. Quick Check:

    Longest prefix with ## + greedy * = B [OK]
Hint: Wildcard * matches greedily with longest prefix ## [OK]
Common Mistakes:
  • Confusing ## (longest) with # (shortest)
  • Underestimating greedy nature of * in longest match
  • Assuming pattern causes error or variable is empty
5. You have a variable path="folder/home/user/docs/report.txt". Which command removes only the first slash and everything before it, leaving home/user/docs/report.txt?
hard
A. echo ${path#/*}
B. echo ${path##/*}
C. echo ${path##*/}
D. echo ${path#*/}

Solution

  1. Step 1: Understand the variable value

    The variable path is "folder/home/user/docs/report.txt".
  2. Step 2: Analyze each option's pattern

    ${path#/*} removes shortest prefix matching slash followed by anything, but path doesn't start with /, so no match.
    ${path##/*} removes longest prefix matching slash-anything, no match.
    ${path##*/} removes longest prefix up to last slash, leaving only filename "report.txt".
    ${path#*/} removes shortest prefix up to first slash ("folder/"), leaving "home/user/docs/report.txt".
  3. Step 3: Choose correct option

    To remove up to the first slash, use ${path#*/}.
  4. Final Answer:

    echo ${path#*/} -> Option D
  5. Quick Check:

    Remove shortest prefix up to first slash = D [OK]
Hint: Use #*/ to remove up to first slash only [OK]
Common Mistakes:
  • Using ##*/ removes up to last slash (only filename)
  • Using #/* or ##/* which don't match without leading slash
  • Confusing prefix removal with suffix removal
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