Bird
0
0

You want to write a bash script that prints the third argument passed to it, regardless of how many arguments are given. Which snippet correctly uses shift to achieve this?

hard🚀 Application Q8 of 15
Bash Scripting - User Input
You want to write a bash script that prints the third argument passed to it, regardless of how many arguments are given. Which snippet correctly uses shift to achieve this?
Ashift 1 echo "$3"
Bshift 2 echo "$1"
Cshift 3 echo "$1"
Decho "$3"
Step-by-Step Solution
Solution:
  1. Step 1: Understand argument positions

    The third argument is originally $3.
  2. Step 2: Use shift to move $3 to $1

    Shifting by 2 removes first two arguments, so $3 becomes $1.
  3. Step 3: Print $1 after shift

    Printing $1 now outputs the original third argument.
  4. Final Answer:

    shift 2 echo "$1" -> Option B
  5. Quick Check:

    shift 2 moves third arg to $1 [OK]
Quick Trick: Shift by N-1 to print Nth argument as $1 [OK]
Common Mistakes:
MISTAKES
  • Shifting too many or too few arguments
  • Printing $3 without shifting
  • Confusing argument positions after shift

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes