Bird
0
0

Given this script snippet:

hard🚀 Application Q9 of 15
Bash Scripting - Functions
Given this script snippet:
function greet {
  echo "Hello, $1"
}
name="Alice"
greet $name

What will be the output and why?
AHello, $1 because variables inside functions are not expanded
BHello, name because $name is treated as literal
CHello, Alice because variable is expanded before function call
DError due to missing parentheses in function definition
Step-by-Step Solution
Solution:
  1. Step 1: Understand variable expansion in Bash

    Variables like $name are expanded before the function call, so greet $name becomes greet Alice.
  2. Step 2: Function uses $1 to print first argument

    The function prints "Hello, $1", so it outputs "Hello, Alice".
  3. Final Answer:

    Hello, Alice because variable is expanded before function call -> Option C
  4. Quick Check:

    Variables expand before function call, $1 holds argument [OK]
Quick Trick: Variables expand before function call; use $1 inside function [OK]
Common Mistakes:
MISTAKES
  • Expecting literal $1 output
  • Thinking variables inside functions don't expand
  • Syntax errors from parentheses in definition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes