Bird
0
0

You want to process all command-line arguments one by one in a bash script using shift. Which script snippet correctly does this?

hard🚀 Application Q15 of 15
Bash Scripting - User Input
You want to process all command-line arguments one by one in a bash script using shift. Which script snippet correctly does this?
Afor arg in "$@"; do shift; echo "$arg"; done
Bwhile [ "$#" -gt 0 ]; do echo "$1"; shift; done
Cshift; while [ "$#" -gt 0 ]; do echo "$1"; done
Dwhile [ "$#" -ge 1 ]; do echo "$2"; shift 2; done
Step-by-Step Solution
Solution:
  1. Step 1: Understand argument count and shifting

    Use while [ "$#" -gt 0 ] to loop while arguments remain.
  2. Step 2: Print first argument and shift each iteration

    Inside loop, print $1 then shift to move to next argument.
  3. Final Answer:

    while [ "$#" -gt 0 ]; do echo "$1"; shift; done -> Option B
  4. Quick Check:

    Loop with shift processes all args one by one [OK]
Quick Trick: Use while loop with $# and shift to process args one by one [OK]
Common Mistakes:
MISTAKES
  • Using for loop with shift inside causing skipped args
  • Not updating argument count in loop condition
  • Shifting by 2 causing missed arguments

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes