Bird
0
0

What will be the output of this script when run as ./script.sh red blue green yellow?

medium📝 Command Output Q4 of 15
Bash Scripting - User Input
What will be the output of this script when run as ./script.sh red blue green yellow?
#!/bin/bash
shift 2
echo "$1 $2"
Ablue green
Bred blue
Cyellow green
Dgreen yellow
Step-by-Step Solution
Solution:
  1. Step 1: Initial arguments

    Arguments are: $1=red, $2=blue, $3=green, $4=yellow
  2. Step 2: Apply shift 2

    After shifting twice, $1 becomes original $3 (green), $2 becomes original $4 (yellow)
  3. Step 3: Output

    Echo prints "$1 $2" which is "green yellow"
  4. Final Answer:

    green yellow -> Option D
  5. Quick Check:

    Shift moves arguments left by specified count [OK]
Quick Trick: Shift N removes first N arguments [OK]
Common Mistakes:
MISTAKES
  • Assuming arguments remain unchanged after shift
  • Mixing order of arguments after shift
  • Confusing $1 and $2 values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes