Bird
0
0

Why does $1 inside a bash function sometimes appear empty even when arguments are passed?

hard🧠 Conceptual Q10 of 15
Bash Scripting - Functions
Why does $1 inside a bash function sometimes appear empty even when arguments are passed?
ABecause $1 is overwritten inside the function
BBecause $1 refers to the script's first argument, not the function's
CBecause the function was called without arguments
DBecause $1 is only set for the main script, not functions
Step-by-Step Solution
Solution:
  1. Step 1: Understand $1 scope in functions

    Inside a function, $1 refers to the first argument passed to that function, not the script.
  2. Step 2: Identify why $1 might be empty

    If the function is called without arguments, $1 is empty, even if the script has arguments.
  3. Final Answer:

    Because the function was called without arguments -> Option C
  4. Quick Check:

    Empty $1 means no function arguments passed [OK]
Quick Trick: Function $1 is empty if no arguments passed to function [OK]
Common Mistakes:
MISTAKES
  • Confusing script arguments with function arguments
  • Assuming $1 always has value
  • Thinking $1 is global variable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes