Bird
Raised Fist0
Bash Scriptingscripting~10 mins

String prefix removal (${var#pattern}) in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - String prefix removal (${var#pattern})
Start with variable var
↓
Apply pattern match from start
↓
Remove shortest matching prefix
↓
Return modified string
↓
End
This flow shows how bash removes the shortest matching prefix pattern from a variable's value.
Execution Sample
Bash Scripting
filename="report_2024.txt"
result=${filename#*_}
echo "$result"
Removes the shortest prefix ending with underscore from filename, outputting '2024.txt'.
Execution Table
StepVariable 'filename'PatternActionResulting ValueOutput
1report_2024.txt"*_"Find shortest prefix ending with '_'report_
2report_2024.txt"*_"Remove prefix 'report_'2024.txt
3report_2024.txtN/AEcho the result2024.txt2024.txt
4N/AN/AEnd of scriptN/AN/A
💡 Pattern matched and shortest prefix removed; script ends after echo.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
filenamereport_2024.txtreport_2024.txtreport_2024.txtreport_2024.txt
resultundefinedundefined2024.txt2024.txt
Key Moments - 2 Insights
Why does the pattern '*_' remove only up to the first underscore and not more?
Because ${var#pattern} removes the shortest match from the start. The pattern '*_' matches the shortest prefix ending with underscore, stopping at the first underscore as shown in execution_table step 1.
What happens if the pattern does not match anything in the variable?
If no match is found, the variable remains unchanged. This is implied because removal only happens when a prefix matches, as shown in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'result' after step 2?
Areport_2024.txt
B2024.txt
Creport_
Dtxt
💡 Hint
Check the 'Resulting Value' column in row for step 2.
At which step does the script output '2024.txt'?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Output' column to find when '2024.txt' is printed.
If the pattern was '${filename##*_}', how would the result change after step 2?
AIt would remove the longest prefix ending with underscore, resulting in 'txt'
BIt would remove the shortest prefix ending with underscore, resulting in '2024.txt'
CIt would remove nothing, result stays 'report_2024.txt'
DIt would remove the entire string, result is empty
💡 Hint
Double # removes longest match; compare with single # behavior in variable_tracker.
Concept Snapshot
Syntax: ${var#pattern}
Removes shortest matching prefix pattern from var's value.
Useful to strip parts before a delimiter.
If no match, var stays unchanged.
Double # (##) removes longest matching prefix.
Example: filename="a_b_c"; echo ${filename#*_} outputs 'b_c'.
Full Transcript
This lesson shows how bash removes the shortest matching prefix from a variable using ${var#pattern}. We start with a variable 'filename' holding 'report_2024.txt'. The pattern '*_' matches the shortest prefix ending with underscore, which is 'report_'. Removing it leaves '2024.txt'. Then echo prints this result. If the pattern does not match, the variable stays the same. Using double # removes the longest matching prefix instead. This helps to extract parts of strings easily in scripts.

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