Bird
0
0

What will be the output of this script if run as ./script.sh apple banana cherry?

medium📝 Command Output Q13 of 15
Bash Scripting - User Input
What will be the output of this script if run as ./script.sh apple banana cherry?
echo $1
shift
echo $1
Aapple\napple
Bbanana\ncherry
Cbanana\napple
Dapple\nbanana
Step-by-Step Solution
Solution:
  1. Step 1: Check initial $1 value

    Before shift, $1 is the first argument: 'apple'.
  2. Step 2: Apply shift and check new $1

    After shift, $1 becomes the second argument: 'banana'.
  3. Final Answer:

    apple\nbanana -> Option D
  4. Quick Check:

    First echo prints first arg, second echo prints second arg after shift [OK]
Quick Trick: First echo shows $1, shift moves args left, second echo shows new $1 [OK]
Common Mistakes:
MISTAKES
  • Assuming shift does not change $1
  • Confusing argument positions after shift
  • Expecting output to be 'banana\ncherry'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes