Bird
0
0

You want a script to print all arguments passed, one per line, using a loop. Which snippet correctly uses command-line arguments?

hard🚀 Application Q8 of 15
Bash Scripting - User Input
You want a script to print all arguments passed, one per line, using a loop. Which snippet correctly uses command-line arguments?
Afor arg in "$@"; do echo "$arg"; done
Bfor arg in $*; do echo $arg; done
Cfor arg in $@; do echo $arg; done
Dfor arg in "$*"; do echo "$arg"; done
Step-by-Step Solution
Solution:
  1. Step 1: Understand difference between $@ and $*

    "$@" treats each argument separately; $* and "$*" combine all arguments into one string.
  2. Step 2: Identify correct loop syntax

    for arg in "$@" loops over each argument correctly, preserving spaces.
  3. Final Answer:

    for arg in "$@"; do echo "$arg"; done -> Option A
  4. Quick Check:

    Use "$@" to loop over arguments safely [OK]
Quick Trick: Use "$@" in loops to handle arguments with spaces [OK]
Common Mistakes:
MISTAKES
  • Using $* without quotes
  • Using "$*" which is one string
  • Not quoting variables in echo

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes