Bird
0
0

What will be the output of this bash script?

medium📝 Command Output Q4 of 15
Bash Scripting - Functions
What will be the output of this bash script?
greet() {
  echo "Hi, $1!"
}
greet Alice
greet Bob
AHi, Alice!\nHi, Alice!
BHi, Alice!\nHi, Bob!
CHi, Bob!\nHi, Bob!
DError: function greet not found
Step-by-Step Solution
Solution:
  1. Step 1: Understand function calls with arguments

    The function greet prints "Hi," followed by the first argument passed.
  2. Step 2: Trace the calls

    First call greet Alice prints "Hi, Alice!". Second call greet Bob prints "Hi, Bob!".
  3. Final Answer:

    Hi, Alice!\nHi, Bob! -> Option B
  4. Quick Check:

    Function calls with arguments produce expected greetings = B [OK]
Quick Trick: Function arguments accessed as $1, $2, etc. [OK]
Common Mistakes:
MISTAKES
  • Assuming $1 changes globally
  • Confusing output order
  • Expecting errors without syntax issues

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes