0
0
Bash Scriptingscripting~10 mins

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

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