Bird
Raised Fist0
Bash Scriptingscripting~10 mins

String length (${#var}) 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 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.

Practice

(1/5)
1. What does the expression ${#var} do in a bash script?
easy
A. It converts the variable var to uppercase.
B. It returns the value of the variable var.
C. It counts the number of words in the variable var.
D. It returns the length of the string stored in the variable var.

Solution

  1. Step 1: Understand the syntax ${#var}

    This syntax in bash returns the length of the string stored in the variable var.
  2. Step 2: Differentiate from other operations

    It does not return the value itself, count words, or change case. It specifically counts characters.
  3. Final Answer:

    It returns the length of the string stored in the variable var. -> Option D
  4. Quick Check:

    Length of string = ${#var} [OK]
Hint: Remember: ${#var} counts characters in var [OK]
Common Mistakes:
  • Confusing ${#var} with $var (value of variable)
  • Thinking it counts words instead of characters
  • Assuming it changes the string content
2. Which of the following is the correct way to get the length of a string variable name in bash?
easy
A. length=${name#}
B. length=$#name
C. length=${#name}
D. length=$name#

Solution

  1. Step 1: Recall correct syntax for string length

    The correct syntax to get string length is ${#variable}, so for name it is ${#name}.
  2. Step 2: Check each option

    length=${name#} uses wrong placement of #, length=$#name uses $ and # incorrectly, length=$name# appends # after variable incorrectly.
  3. Final Answer:

    length=${#name} -> Option C
  4. Quick Check:

    Use ${#var} to get length [OK]
Hint: Use ${#var} exactly to get string length [OK]
Common Mistakes:
  • Placing # after variable name instead of before
  • Using $# which counts positional parameters, not string length
  • Confusing variable expansion syntax
3. What will be the output of the following bash script?
text="Hello World"
echo ${#text}
medium
A. 11
B. Error
C. 12
D. 10

Solution

  1. Step 1: Count characters in the string "Hello World"

    The string has 11 characters: 5 letters in "Hello", 1 space, and 5 letters in "World".
  2. Step 2: Understand ${#text} returns string length

    The command echo ${#text} prints the length, which is 11.
  3. Final Answer:

    11 -> Option A
  4. Quick Check:

    Length of "Hello World" = 11 [OK]
Hint: Count spaces too when using ${#var} [OK]
Common Mistakes:
  • Ignoring spaces in length count
  • Counting words instead of characters
  • Expecting output to be string value instead of length
4. Consider this bash script snippet:
myvar=Hello
length=$#myvar
echo $length

What is the problem with this script?
medium
A. It will print the number of positional parameters, not string length.
B. It will print the length of the string correctly.
C. It will cause a syntax error due to wrong variable name.
D. It will print the string value instead of length.

Solution

  1. Step 1: Understand what $# means in bash

    The symbol $# returns the number of positional parameters passed to the script, not the length of a string variable.
  2. Step 2: Analyze the script behavior

    Assigning length=$#myvar tries to get positional parameters count, ignoring myvar. So it does not get string length.
  3. Final Answer:

    It will print the number of positional parameters, not string length. -> Option A
  4. Quick Check:

    $# counts parameters, not string length [OK]
Hint: Use ${#var} for length, not $# [OK]
Common Mistakes:
  • Confusing $# (positional params) with ${#var} (string length)
  • Expecting $# to work on variables
  • Missing braces around variable name
5. You want to check if a user input stored in variable input is empty or not in a bash script. Which condition correctly uses string length to do this?
hard
A. if [ $#input -eq 0 ]; then echo "Empty input"; fi
B. if [ ${#input} -eq 0 ]; then echo "Empty input"; fi
C. if [ $input -eq 0 ]; then echo "Empty input"; fi
D. if [ ${input#} -eq 0 ]; then echo "Empty input"; fi

Solution

  1. Step 1: Use ${#input} to get string length

    To check if input is empty, we check if its length is zero using ${#input}.
  2. Step 2: Write correct if condition syntax

    The condition [ ${#input} -eq 0 ] correctly tests if the string length is zero.
  3. Final Answer:

    if [ ${#input} -eq 0 ]; then echo "Empty input"; fi -> Option B
  4. Quick Check:

    Check empty string with ${#var} == 0 [OK]
Hint: Test empty string with if [ ${#var} -eq 0 ] [OK]
Common Mistakes:
  • Using $#input which is invalid
  • Comparing string value directly to number
  • Misplacing # symbol in variable expansion
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