Bird
Raised Fist0
Bash Scriptingscripting~10 mins

String prefix removal (${var#pattern}) in Bash Scripting - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to remove the shortest prefix 'abc' from the variable.

Bash Scripting
result=${var[1]abc}
Drag options to blanks, or click blank then click option'
A##
B#
C%
D%%
Attempts:
3 left
💡 Hint
Common Mistakes
Using ## removes the longest prefix instead of the shortest.
Using % or %% removes suffixes, not prefixes.
2fill in blank
medium

Complete the code to remove the shortest prefix matching any characters up to 'end'.

Bash Scripting
result=${var[1]*end}
Drag options to blanks, or click blank then click option'
A#
B%
C##
D%%
Attempts:
3 left
💡 Hint
Common Mistakes
Using ## removes the longest prefix, which may remove more than intended.
Using % or %% removes suffixes, not prefixes.
3fill in blank
hard

Fix the error in the code to remove the shortest prefix 'temp_' from the variable.

Bash Scripting
cleaned=${var[1]temp_}
Drag options to blanks, or click blank then click option'
A%%
B%
C#
D##
Attempts:
3 left
💡 Hint
Common Mistakes
Using % or %% tries to remove suffixes, causing no change.
Using ## removes the longest prefix, which may remove more than intended.
4fill in blank
hard

Fill both blanks to remove the shortest prefix matching 'log_' and assign to output.

Bash Scripting
output=$[1][2]log_}
Drag options to blanks, or click blank then click option'
Avar
B##
C#
Dfile
Attempts:
3 left
💡 Hint
Common Mistakes
Using ## removes the longest prefix, which is not correct here.
Using wrong variable names like 'file' causes errors.
5fill in blank
hard

Fill all three blanks to create a dictionary with keys as filenames without 'tmp_' prefix and values as their sizes.

Bash Scripting
declare -A sizes; for [3] in tmp_files; do sizes[[1]]=$(stat -c%s [2]); done
Drag options to blanks, or click blank then click option'
A${file#tmp_}
B$file
Cfile
D${file##tmp_}
Attempts:
3 left
💡 Hint
Common Mistakes
Using ${file##tmp_} removes the longest prefix, which may remove more than intended.
Using different variable names inconsistently causes errors.

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