Question 94 of 537
Create simple shell scriptsmediumMultiple ChoiceObjective-mapped

Variable Scope in Bash Functions: local vs declare -g

This EX200 practice question tests your understanding of create simple shell scripts. The scenario asks you to isolate a root cause — eliminate options that address a different problem before choosing. A key principle to apply: local keyword. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.

A junior admin writes a script that uses functions. They notice that a variable set inside a function is not available after the function call. What is the likely cause and best practice?

Clue words in this question

Noticing these words before you look at the options changes how you read each choice.

  • Clue: "best"

    Why it matters: Signals that multiple options may be partially correct. Choose the option that most directly solves the exact problem described, not the one that sounds most complete.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

In bash, variables declared in a function are local by default, use 'declare -g' to make them global

In Bash, variables are global by default unless explicitly declared with the 'local' keyword. The scenario describes a variable that is not available after the function call, which typically happens when the variable was declared with 'local'. To make a variable global even when declared inside a function, the best practice is to use 'declare -g', which explicitly sets the variable's scope to global. Option D correctly identifies this best practice. The other options are incorrect: Option A wrongly suggests using a 'global' keyword (which doesn't exist), Option B misunderstands sourcing (which only affects variable visibility in the current shell, not function scope), and Option C incorrectly attributes the issue to subshells when the function itself is not run in a subshell.

Key principle: local keyword

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • The variable was declared with 'local', change to 'global' keyword

    Why it's wrong here

    There is no 'global' keyword in bash. To make a variable global inside a function, either avoid using 'local' or use 'declare -g'.

  • The script must be sourced (.) instead of executed to retain variables

    Why it's wrong here

    Incorrect. Sourcing the script would make the function's variables available in the current shell if they are not declared local, but the issue is about function-local variables, not script execution context.

  • The function was called inside a subshell, use 'export' to make it global

    Why it's wrong here

    Incorrect. 'export' exports a variable to the environment, but does not change its scope. If the variable is local, exporting won't make it available outside the function. Also, calling in a subshell would not affect variable visibility after the function returns.

  • In bash, variables declared in a function are local by default, use 'declare -g' to make them global

    Why this is correct

    Correct. In bash, variables inside a function are global by default unless declared with 'local'. However, if a variable is declared with 'local', it is local. To explicitly make it global from within a function, use 'declare -g'. This is the best practice.

    Clue confirmation

    The clue word "best" in the question point toward this answer.

    Related concept

    local keyword

Common exam traps

Common exam trap: answer the scenario, not the keyword

A common pitfall in RHCSA is assuming that all variables in a function are local. In fact, they are global unless 'local' is used. The trap here is that candidates may think that 'local' makes a variable available globally (Option A), or that sourcing is needed (Option B), or that 'export' fixes it (Option C). The correct approach to make a variable global from within a function is 'declare -g'.

Trap categories for this question

  • Keyword trap

    There is no 'global' keyword in bash. To make a variable global inside a function, either avoid using 'local' or use 'declare -g'.

Detailed technical explanation

How to think about this question

Bash functions do not create a new scope unless the 'local' keyword is used; variables are global by default. However, if a function is called within a subshell (e.g., via parentheses or a pipeline), any variable assignments, even global ones, are lost when the subshell exits. The 'declare -g' option forces a variable to be global even when used inside a function, but it does not survive a subshell. A real-world scenario is a script that sets a configuration variable inside a function and expects it to persist — using 'declare -g' ensures the variable is available after the function returns, provided the function is not run in a subshell.

KKey Concepts to Remember

  • local keyword
  • declare -g
  • Global variable
  • Function scope

TExam Day Tips

  • Watch for words such as best, first, most likely and least administrative effort.
  • Review why wrong options are wrong, not only why the correct option is correct.

Key takeaway

local keyword

Real-world example

How this comes up in practice

A practitioner preparing for the EX200 exam encounters this exact type of scenario on the job. The correct answer here is not the most general option — it is the best answer for the specific constraint described. local keyword Real exam questions reward reading the full scenario before eliminating options, because the constraint defines which answer fits.

What to study next

Got this wrong? Here's your next step.

Review local keyword, then practise related EX200 questions on the same topic to reinforce the concept.

Related practice questions

Related EX200 practice-question pages

Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.

Practice this exam

Start a free EX200 practice session

Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.

FAQ

Questions learners often ask

What does this EX200 question test?

Create simple shell scripts — This question tests Create simple shell scripts — local keyword.

What is the correct answer to this question?

The correct answer is: In bash, variables declared in a function are local by default, use 'declare -g' to make them global — In Bash, variables are global by default unless explicitly declared with the 'local' keyword. The scenario describes a variable that is not available after the function call, which typically happens when the variable was declared with 'local'. To make a variable global even when declared inside a function, the best practice is to use 'declare -g', which explicitly sets the variable's scope to global. Option D correctly identifies this best practice. The other options are incorrect: Option A wrongly suggests using a 'global' keyword (which doesn't exist), Option B misunderstands sourcing (which only affects variable visibility in the current shell, not function scope), and Option C incorrectly attributes the issue to subshells when the function itself is not run in a subshell.

What should I do if I get this EX200 question wrong?

Review local keyword, then practise related EX200 questions on the same topic to reinforce the concept.

Are there clue words in this question I should notice?

Yes — watch for: "best". Signals that multiple options may be partially correct. Choose the option that most directly solves the exact problem described, not the one that sounds most complete.

What is the key concept behind this question?

local keyword

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Keep practising

More EX200 practice questions

Last reviewed: Jul 4, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

This EX200 practice question is part of Courseiva's free Red Hat certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the EX200 exam.