Bird
0
0

What will be the output of this script when run as ./script.sh apple?

medium📝 Command Output Q5 of 15
Bash Scripting - User Input
What will be the output of this script when run as ./script.sh apple?
if [ "$1" = "apple" ]; then
  echo "Fruit is apple"
else
  echo "Fruit is not apple"
fi
AFruit is apple
BFruit is not apple
CSyntax error
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Check the value of $1

    $1 is "apple" as passed in command line.
  2. Step 2: Evaluate the if condition

    The condition [ "$1" = "apple" ] is true, so it prints "Fruit is apple".
  3. Final Answer:

    Fruit is apple -> Option A
  4. Quick Check:

    If $1 equals apple, output is "Fruit is apple" [OK]
Quick Trick: Use quotes around $1 to avoid errors with empty args [OK]
Common Mistakes:
MISTAKES
  • Missing quotes causing errors
  • Using == instead of = in [ ]
  • Not handling empty $1

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes