Bird
0
0

What will be the output of this script if run as ./script.sh dog cat?

medium📝 Command Output Q4 of 15
Bash Scripting - User Input
What will be the output of this script if run as ./script.sh dog cat?
echo "First: $1"
echo "Second: $2"
echo "Count: $#"
AFirst: cat Second: dog Count: 2
BFirst: dog Second: cat Count: 2
CFirst: dog Second: cat Count: 3
DFirst: ./script.sh Second: dog Count: 2
Step-by-Step Solution
Solution:
  1. Step 1: Identify the arguments passed

    Arguments are dog and cat, so $1=dog, $2=cat.
  2. Step 2: Count the number of arguments

    There are 2 arguments, so $#=2.
  3. Final Answer:

    First: dog Second: cat Count: 2 -> Option B
  4. Quick Check:

    Arguments and count match output A [OK]
Quick Trick: Remember $1 and $2 are first and second arguments [OK]
Common Mistakes:
MISTAKES
  • Swapping $1 and $2
  • Counting script name as argument
  • Miscounting $#

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes