Bird
0
0

How can you get the length of the first word in a string variable line="hello world" using bash string length?

hard🚀 Application Q9 of 15
Bash Scripting - String Operations
How can you get the length of the first word in a string variable line="hello world" using bash string length?
Aecho ${#line##* }
Becho ${#line#* }
Cecho ${#line% *}
Decho ${#line%% *}
Step-by-Step Solution
Solution:
  1. Step 1: Extract first word using parameter expansion

    The expression ${line%% *} removes longest match of space and after, leaving first word.
  2. Step 2: Use ${#...} to get length of extracted word

    Combining, ${#line%% *} gives length of first word.
  3. Final Answer:

    echo ${#line%% *} -> Option D
  4. Quick Check:

    Use %% to trim after space, then get length [OK]
Quick Trick: Use ${#var%% *} to get length of first word [OK]
Common Mistakes:
MISTAKES
  • Using # or % incorrectly for word extraction
  • Getting length of whole string instead of first word
  • Confusing single and double % operators

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes