0
0
Bash Scriptingscripting~10 mins

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

Choose your learning style9 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.