Bird
0
0

Which of the following bash commands correctly outputs the length of the string stored in variable message?

easy📝 Syntax Q3 of 15
Bash Scripting - String Operations
Which of the following bash commands correctly outputs the length of the string stored in variable message?
Aecho $message.length
Becho ${#message}
Cecho length($message)
Decho ${message#length}
Step-by-Step Solution
Solution:
  1. Step 1: Recall bash syntax for string length

    In bash, ${#var} returns the length of the string in var.
  2. Step 2: Evaluate options

    Only echo ${#message} uses the correct syntax. Options B, C, and D are invalid bash syntax for string length.
  3. Final Answer:

    echo ${#message} -> Option B
  4. Quick Check:

    Test with message="test" outputs 4 [OK]
Quick Trick: Use echo ${#var} to get string length [OK]
Common Mistakes:
MISTAKES
  • Using dot notation like $var.length which is invalid in bash
  • Trying to call length() as a function
  • Misusing parameter expansion syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes