Bird
0
0

You want to create a function greet that prints "Hello" followed by the current username. Which is the correct function?

hard🚀 Application Q8 of 15
Bash Scripting - Functions
You want to create a function greet that prints "Hello" followed by the current username. Which is the correct function?
Agreet() { echo "Hello $USERNAME"; }
Bgreet() { echo "Hello $user"; }
Cgreet() { echo "Hello $USER"; }
Dgreet() { echo "Hello $(whoami)"; }
Step-by-Step Solution
Solution:
  1. Step 1: Understand environment variables and commands

    $USER is usually set but may not always be reliable; $(whoami) runs the command to get the current username.
  2. Step 2: Evaluate each option

    greet() { echo "Hello $USER"; } uses $USER which may work but is environment-dependent. greet() { echo "Hello $user"; } uses lowercase $user which is usually undefined. greet() { echo "Hello $(whoami)"; } uses $(whoami) which always returns the current username. greet() { echo "Hello $USERNAME"; } uses $USERNAME which is not standard in bash.
  3. Final Answer:

    greet() { echo "Hello $(whoami)"; } -> Option D
  4. Quick Check:

    Use $(whoami) for reliable current username [OK]
Quick Trick: Use $(whoami) to get current username reliably [OK]
Common Mistakes:
MISTAKES
  • Using undefined variables
  • Assuming $USER always set
  • Forgetting command substitution syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes