0
0
Bash Scriptingscripting~10 mins

String length (${#var}) 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 print the length of the string stored in variable name.

Bash Scripting
echo [1]
Drag options to blanks, or click blank then click option'
A$name
B$#name
C${name#}
D${#name}
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.
2fill in blank
medium

Complete 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'
A${word#}
B$word
C${#word}
D$#word
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.
3fill in blank
hard

Fix 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'
A${#text}
B${text#}
C$#text
D${text}
Attempts:
3 left
💡 Hint
Common Mistakes
Using ${text} prints the string, not its length.
Using $#text is invalid syntax.
Using ${text#} is incorrect.
4fill in blank
hard

Fill 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'
A"${words[@]}"
B${#word}
C${word}
Dwords
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.
5fill in blank
hard

Fill 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'
Aitem
B"${list[@]}"
C${#item}
Dlist
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.