Courseiva

EX200 Create simple shell scripts Practice Question

Exhibit

Refer to the exhibit.
```bash
#!/bin/bash
# Script to set environment variable
MY_VAR="hello"
export MY_VAR
```

A user executes the script shown in the exhibit with './export_script.sh' and then runs 'echo $MY_VAR' in the same terminal. The output is empty. Why does this happen?

⚠ Common exam trap

It's easy for candidates to confuse 'export' with making a variable globally available across all shells, not realizing that export only propagates to child processes, not to the parent shell that executed the script.

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

✓

The script runs in a sub-shell, so exported variables are not available to the parent shell

When a script is executed with './export_script.sh', it runs in a sub-shell (a child process). The 'export' command within the script makes the variable available to that sub-shell and its own child processes, but not to the parent shell that invoked the script. Therefore, after the script exits, the variable MY_VAR is not defined in the parent shell's environment, resulting in an empty output from 'echo $MY_VAR'.

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 script lacks execute permissions

    Why it's wrong here

    If the script lacked execute permissions, the shell would refuse to run it with ./script and return 'Permission denied'. Since the user was able to execute it and observe the described behavior, the execute bit must be set; therefore permission is not the cause. The question is about variable scope, not file mode.

  • ✓

    The script runs in a sub-shell, so exported variables are not available to the parent shell

    Why this is correct

    When you execute a script by name or with ./script, the kernel starts a new shell process (child) with its own copy of the parent's environment. The export builtin marks VARIABLE for inheritance by that child, but any assignments or exports made inside the script modify only the child's environment, which is discarded when the script exits. Therefore the exported variable never appears in the parent shell, even though it is available to programs launched within the script. This is the fundamental process isolation you are observing.

  • ✗

    The 'export' command is incorrectly placed after the variable assignment

    Why it's wrong here

    The placement of export after the assignment is perfectly valid syntax. Both forms — `VAR=value; export VAR` and `export VAR=value` — are equivalent in bash and fully export the variable to the current shell's environment. The issue here is not the order of these commands, because in either case the script runs in a sub-shell whose exports vanish when the process ends. Thus the ordering cannot explain why the parent does not see the variable.

  • ✗

    The variable name should be in uppercase for it to be inherited

    Why it's wrong here

    Environment variable names are case-sensitive, but the shell does not require uppercase names for inheritance; `export myvar` works identically to `export MYVAR` as long as you are consistent. The conventional ALL_CAPS style is only a human-readable guideline to avoid collisions with internal shell variables. Therefore changing the variable to uppercase would not change the fact that a script's exports are confined to the child process and its descendants.

About these practice questions

This EX200 question is part of Courseiva's 427-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

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.