Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print the length of the string stored in variable name.
Bash Scripting
echo [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using just $name prints the string, not its length.
Using ${name#} is incorrect syntax for length.
Using $#name is invalid in this context.
✗ Incorrect
In bash,
${#var} gives the length of the string stored in var. So ${#name} prints the length of name.2fill in blank
mediumComplete the code to store the length of word into variable len.
Bash Scripting
len=[1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning
len=$word stores the string, not its length.Using
${word#} is invalid for length.Using
$#word is incorrect syntax.✗ Incorrect
Assigning
len=${#word} stores the length of word in len.3fill in blank
hardFix the error in the code to correctly print the length of text.
Bash Scripting
echo [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
${text} prints the string, not its length.Using
$#text is invalid syntax.Using
${text#} is incorrect.✗ Incorrect
Only
${#text} correctly prints the length of the string stored in text.4fill in blank
hardFill both blanks to create a loop that prints each word and its length from the list words.
Bash Scripting
for word in [1]; do echo "$word: $[2]" done
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using just
words without quotes or array syntax causes errors.Using
${word} instead of ${#word} prints the word, not its length.✗ Incorrect
The loop iterates over all elements in the array
words using "${words[@]}". Inside the loop, ${#word} gives the length of the current word.5fill in blank
hardFill all three blanks to create a dictionary-like output of words and their lengths from list.
Bash Scripting
declare -A lengths for [1] in [2]; do lengths[[1]]=[3] done for key in "${!lengths[@]}"; do echo "$key: ${lengths[$key]}" done
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
list without quotes or array syntax causes errors.Using
${item} instead of ${#item} stores the word, not its length.Using inconsistent variable names inside the loop.
✗ Incorrect
The loop uses
item as the variable, iterates over all elements in list with "${list[@]}", and stores the length of each item with ${#item}.